|
| 1 | +const startBtn = document.getElementById('start-btn'); |
| 2 | +const resetBtn = document.getElementById('reset-btn'); |
| 3 | +const minutesDisplay = document.getElementById('minutes'); |
| 4 | +const secondsDisplay = document.getElementById('seconds'); |
| 5 | +const statusDisplay = document.getElementById('status'); |
| 6 | + |
| 7 | +let timerInterval; |
| 8 | +let isFocusSession = true; // Start with a focus session |
| 9 | +const focusTime = 5 * 60; // 5 minutes in seconds |
| 10 | +const breakTime = 5 * 60; // 5 minutes in seconds |
| 11 | +let timeRemaining = focusTime; |
| 12 | + |
| 13 | +function updateDisplay() { |
| 14 | + const minutes = Math.floor(timeRemaining / 60); |
| 15 | + const seconds = timeRemaining % 60; |
| 16 | + minutesDisplay.textContent = String(minutes).padStart(2, '0'); |
| 17 | + secondsDisplay.textContent = String(seconds).padStart(2, '0'); |
| 18 | +} |
| 19 | + |
| 20 | +function toggleSession() { |
| 21 | + isFocusSession = !isFocusSession; |
| 22 | + timeRemaining = isFocusSession ? focusTime : breakTime; |
| 23 | + statusDisplay.textContent = isFocusSession |
| 24 | + ? 'Focus Session' |
| 25 | + : 'Break Session'; |
| 26 | + updateDisplay(); |
| 27 | +} |
| 28 | + |
| 29 | +function startTimer() { |
| 30 | + if (timerInterval) return; // Prevent multiple intervals |
| 31 | + timerInterval = setInterval(() => { |
| 32 | + if (timeRemaining > 0) { |
| 33 | + timeRemaining -= 1; |
| 34 | + updateDisplay(); |
| 35 | + } else { |
| 36 | + clearInterval(timerInterval); |
| 37 | + timerInterval = null; |
| 38 | + toggleSession(); |
| 39 | + } |
| 40 | + }, 1000); |
| 41 | +} |
| 42 | + |
| 43 | +function resetTimer() { |
| 44 | + clearInterval(timerInterval); |
| 45 | + timerInterval = null; |
| 46 | + timeRemaining = isFocusSession ? focusTime : breakTime; |
| 47 | + updateDisplay(); |
| 48 | +} |
| 49 | + |
| 50 | +startBtn.addEventListener('click', startTimer); |
| 51 | +resetBtn.addEventListener('click', resetTimer); |
| 52 | + |
| 53 | +// Initialize display |
| 54 | +updateDisplay(); |
0 commit comments