Untitled
plain_text
23 days ago
2.5 kB
4
Indexable
Never
const countdownElement = document.getElementById('countdown'); const stopwatchElement = document.getElementById('stopwatch'); const bodyElement = document.body; // Get the body element let countdownTimeLeft = 1 * 20; // 5 minutes in seconds let stopwatchTimeElapsed = 0; // in seconds let countdownInterval; let stopwatchInterval; let timersStarted = false; let shouldContinueTimers = true; // Flag to control timer continuation function updateCounter() { const countdownMinutes = Math.floor(countdownTimeLeft / 60); const countdownSeconds = countdownTimeLeft % 60; countdownElement.textContent = `${countdownMinutes}:${countdownSeconds.toString().padStart(2, '0')}`; } function startCountdown() { countdownInterval = setInterval(() => { if (countdownTimeLeft > 0 && shouldContinueTimers) { countdownTimeLeft--; if (countdownTimeLeft === 15) { // Change background color to orange when 15 seconds left countdownElement.style.backgroundColor = 'orange'; countdownElement.style.color = 'black'; bodyElement.style.backgroundColor = 'orange'; // Change the full screen background color } updateCounter(); } else if (countdownTimeLeft === 0) { clearInterval(countdownInterval); countdownElement.style.backgroundColor = 'red'; countdownElement.style.color = 'black'; countdownElement.classList.remove('clickable'); bodyElement.style.backgroundColor = 'red'; // Change the full screen background color } }, 1000); } function updateStopwatch() { const stopwatchMinutes = Math.floor(stopwatchTimeElapsed / 60); const stopwatchSeconds = stopwatchTimeElapsed % 60; stopwatchElement.textContent = `${stopwatchMinutes}:${stopwatchSeconds.toString().padStart(2, '0')}`; stopwatchTimeElapsed++; } function startStopwatch() { stopwatchInterval = setInterval(() => { if (shouldContinueTimers) { updateStopwatch(); } }, 1000); } function stopTimers() { clearInterval(countdownInterval); clearInterval(stopwatchInterval); } // Add a click event listener to countdownElement to toggle timer continuation countdownElement.addEventListener('click', () => { if (timersStarted && countdownTimeLeft > 15) { shouldContinueTimers = !shouldContinueTimers; } }); document.addEventListener('click', () => { if (!timersStarted) { startCountdown(); startStopwatch(); timersStarted = true; } }); // Update the counters initially to avoid 0:00 display updateCounter(); updateStopwatch();