Add ability to cancel timer

This commit is contained in:
Joey Yakimowich-Payne 2025-01-30 17:31:04 -07:00
commit 3125291621
No known key found for this signature in database
GPG key ID: 6BFE655FA5ABD1E1
2 changed files with 28 additions and 8 deletions

View file

@ -27,6 +27,6 @@
</div> </div>
</div> </div>
<script src="js/index.js?v=1"></script> <script src="js/index.js?v=2"></script>
</body> </body>
</html> </html>

View file

@ -11,6 +11,7 @@ document.addEventListener('DOMContentLoaded', async () => {
const timerDisplay = document.getElementById('timerDisplay'); const timerDisplay = document.getElementById('timerDisplay');
const alertSound = new Audio("audio/done.wav"); const alertSound = new Audio("audio/done.wav");
let timerInterval; let timerInterval;
let isTimerRunning = false;
function getRandomTopics(count) { function getRandomTopics(count) {
const shuffled = [...topics].sort(() => 0.5 - Math.random()); const shuffled = [...topics].sort(() => 0.5 - Math.random());
@ -37,6 +38,17 @@ document.addEventListener('DOMContentLoaded', async () => {
return `${mins}:${secs.toString().padStart(2, '0')}`; return `${mins}:${secs.toString().padStart(2, '0')}`;
} }
function resetTimer() {
clearInterval(timerInterval);
timerDisplay.textContent = '';
timerBtn.textContent = 'Start Timer';
timerBtn.classList.remove('bg-red-600', 'hover:bg-red-700');
timerBtn.classList.add('bg-green-600', 'hover:bg-green-700');
timerBtn.disabled = false;
timerBtn.classList.remove('opacity-50');
isTimerRunning = false;
}
function startTimer(duration, callback) { function startTimer(duration, callback) {
let timeLeft = duration; let timeLeft = duration;
timerDisplay.textContent = formatTime(timeLeft); timerDisplay.textContent = formatTime(timeLeft);
@ -47,29 +59,37 @@ document.addEventListener('DOMContentLoaded', async () => {
if (timeLeft <= 0) { if (timeLeft <= 0) {
clearInterval(timerInterval); clearInterval(timerInterval);
alertSound.play(); playAlertSound();
callback?.(); callback?.();
} }
}, 1000); }, 1000);
} }
function playAlertSound() {
alertSound.play();
}
generateBtn.addEventListener('click', () => { generateBtn.addEventListener('click', () => {
const selectedTopics = getRandomTopics(3); const selectedTopics = getRandomTopics(3);
displayTopics(selectedTopics); displayTopics(selectedTopics);
}); });
timerBtn.addEventListener('click', () => { timerBtn.addEventListener('click', () => {
// Clear any existing timer if (isTimerRunning) {
clearInterval(timerInterval); resetTimer();
timerBtn.disabled = true; return;
timerBtn.classList.add('opacity-50'); }
isTimerRunning = true;
timerBtn.textContent = 'Cancel Timer';
timerBtn.classList.remove('bg-green-600', 'hover:bg-green-700');
timerBtn.classList.add('bg-red-600', 'hover:bg-red-700');
// Start 15-second timer // Start 15-second timer
startTimer(15, () => { startTimer(15, () => {
// After 15-second timer, start 1-minute timer // After 15-second timer, start 1-minute timer
startTimer(60, () => { startTimer(60, () => {
timerBtn.disabled = false; resetTimer();
timerBtn.classList.remove('opacity-50');
}); });
}); });
}); });