|
| 1 | +// ========================================== |
| 2 | +// 1. VARIABLES & CONFIGURATION |
| 3 | +// ========================================== |
| 4 | + |
| 5 | +// Time durations in seconds |
| 6 | +const FOCUS_TIME = 25 * 60; |
| 7 | +const SHORT_BREAK_TIME = 5 * 60; |
| 8 | +const LONG_BREAK_TIME = 15 * 60; |
| 9 | + |
| 10 | +// Colors |
| 11 | +const COLOR_BLUE = "var(--google-blue)"; |
| 12 | +const COLOR_GREEN = "var(--google-green)"; |
| 13 | +const COLOR_YELLOW = "var(--google-yellow)"; |
| 14 | + |
| 15 | +// State variables (Global) |
| 16 | +let timeLeft = FOCUS_TIME; |
| 17 | +let isRunning = false; |
| 18 | +let currentMode = "focus"; // 'focus', 'short-break', 'long-break' |
| 19 | +let timerInterval = null; |
| 20 | + |
| 21 | + |
| 22 | +// ========================================== |
| 23 | +// 2. DOM ELEMENTS (Selecting by ID) |
| 24 | +// ========================================== |
| 25 | + |
| 26 | +const timerDisplay = document.getElementById("timer-display"); |
| 27 | +const timerLabel = document.getElementById("timer-label"); |
| 28 | +const ringProgress = document.getElementById("ring-progress"); |
| 29 | + |
| 30 | +// Buttons |
| 31 | +const startBtn = document.getElementById("toggle-btn"); |
| 32 | +const resetBtn = document.getElementById("reset-btn"); |
| 33 | +const toggleIcon = document.getElementById("toggle-icon"); |
| 34 | + |
| 35 | +// Mode Buttons |
| 36 | +const focusBtn = document.getElementById("focus-btn"); |
| 37 | +const shortBreakBtn = document.getElementById("short-break-btn"); |
| 38 | +const longBreakBtn = document.getElementById("long-break-btn"); |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +// ========================================== |
| 43 | +// 3. TIMER FUNCTIONS |
| 44 | +// ========================================== |
| 45 | + |
| 46 | +function updateTimerDisplay() { |
| 47 | + // Calculate minutes and seconds |
| 48 | + const minutes = Math.floor(timeLeft / 60); |
| 49 | + const seconds = timeLeft % 60; |
| 50 | + |
| 51 | + // Format as "MM:SS" (e.g., "05:09") |
| 52 | + const formattedTime = |
| 53 | + minutes.toString().padStart(2, "0") + |
| 54 | + ":" + |
| 55 | + seconds.toString().padStart(2, "0"); |
| 56 | + |
| 57 | + timerDisplay.textContent = formattedTime; |
| 58 | + |
| 59 | + // Update the ring progress |
| 60 | + let totalTime = FOCUS_TIME; |
| 61 | + if (currentMode === "short-break") totalTime = SHORT_BREAK_TIME; |
| 62 | + if (currentMode === "long-break") totalTime = LONG_BREAK_TIME; |
| 63 | + |
| 64 | + const progress = 1 - timeLeft / totalTime; |
| 65 | + ringProgress.style.strokeDashoffset = progress; |
| 66 | +} |
| 67 | + |
| 68 | +function startTimer() { |
| 69 | + if (isRunning) { |
| 70 | + // If already running, pause it |
| 71 | + clearInterval(timerInterval); |
| 72 | + isRunning = false; |
| 73 | + toggleIcon.textContent = "play_arrow"; |
| 74 | + timerLabel.textContent = "Paused"; |
| 75 | + console.log("Timer Paused"); |
| 76 | + } else { |
| 77 | + // Start the timer |
| 78 | + isRunning = true; |
| 79 | + toggleIcon.textContent = "pause"; |
| 80 | + timerLabel.textContent = |
| 81 | + currentMode === "focus" ? "Stay focused" : "Take a break"; |
| 82 | + |
| 83 | + timerInterval = setInterval(() => { |
| 84 | + if (timeLeft > 0) { |
| 85 | + timeLeft = timeLeft - 1; |
| 86 | + console.log("Timer Running"); |
| 87 | + updateTimerDisplay(); |
| 88 | + } else { |
| 89 | + // Timer finished |
| 90 | + clearInterval(timerInterval); |
| 91 | + isRunning = false; |
| 92 | + toggleIcon.textContent = "play_arrow"; |
| 93 | + alert("Time is up!"); |
| 94 | + console.log("TImer Finished"); |
| 95 | + } |
| 96 | + }, 1000); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +function resetTimer() { |
| 101 | + clearInterval(timerInterval); |
| 102 | + isRunning = false; |
| 103 | + toggleIcon.textContent = "play_arrow"; |
| 104 | + console.log("Timer Reset"); |
| 105 | + // Reset time based on current mode |
| 106 | + if (currentMode === "focus") timeLeft = FOCUS_TIME; |
| 107 | + else if (currentMode === "short-break") timeLeft = SHORT_BREAK_TIME; |
| 108 | + else if (currentMode === "long-break") timeLeft = LONG_BREAK_TIME; |
| 109 | + |
| 110 | + updateTimerDisplay(); |
| 111 | +} |
| 112 | + |
| 113 | +function setMode(mode) { |
| 114 | + currentMode = mode; |
| 115 | + |
| 116 | + // Update buttons style |
| 117 | + focusBtn.classList.remove("active"); |
| 118 | + shortBreakBtn.classList.remove("active"); |
| 119 | + longBreakBtn.classList.remove("active"); |
| 120 | + |
| 121 | + const root = document.documentElement; |
| 122 | + console.log(root); |
| 123 | + |
| 124 | + if (mode === "focus") { |
| 125 | + timeLeft = FOCUS_TIME; |
| 126 | + focusBtn.classList.add("active"); |
| 127 | + root.style.setProperty("--theme-primary", COLOR_BLUE); |
| 128 | + timerLabel.textContent = "Ready to focus?"; |
| 129 | + console.log("Focus Mode"); |
| 130 | + } else if (mode === "short-break") { |
| 131 | + timeLeft = SHORT_BREAK_TIME; |
| 132 | + shortBreakBtn.classList.add("active"); |
| 133 | + root.style.setProperty("--theme-primary", COLOR_GREEN); |
| 134 | + timerLabel.textContent = "Time for a break"; |
| 135 | + console.log("Short Break Mode"); |
| 136 | + } else if (mode === "long-break") { |
| 137 | + timeLeft = LONG_BREAK_TIME; |
| 138 | + longBreakBtn.classList.add("active"); |
| 139 | + root.style.setProperty("--theme-primary", COLOR_YELLOW); |
| 140 | + timerLabel.textContent = "Time for a long break"; |
| 141 | + console.log("Long Break Mode"); |
| 142 | + } |
| 143 | + |
| 144 | + // Stop timer when switching modes |
| 145 | + clearInterval(timerInterval); |
| 146 | + isRunning = false; |
| 147 | + toggleIcon.textContent = "play_arrow"; |
| 148 | + console.log("Timer Reset"); |
| 149 | + |
| 150 | + updateTimerDisplay(); |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | +// ========================================== |
| 156 | +// 5. EVENT LISTENERS |
| 157 | +// ========================================== |
| 158 | + |
| 159 | +startBtn.addEventListener("click", startTimer); |
| 160 | +resetBtn.addEventListener("click", resetTimer); |
| 161 | + |
| 162 | +focusBtn.addEventListener("click", () => { |
| 163 | + setMode("focus"); |
| 164 | + console.log("Focus mode activated"); |
| 165 | +}); |
| 166 | + |
| 167 | +shortBreakBtn.addEventListener("click", () => { |
| 168 | + setMode("short-break"); |
| 169 | + console.log("Short break mode activated"); |
| 170 | +}); |
| 171 | + |
| 172 | +longBreakBtn.addEventListener("click", () => { |
| 173 | + setMode("long-break"); |
| 174 | + console.log("Long break mode activated"); |
| 175 | +}); |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +// Initialize |
| 180 | +updateTimerDisplay(); |
| 181 | + |
0 commit comments