diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..394864b7b 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,48 @@ -function setAlarm() {} +let countdown = null; + +function updateDisplay(time) { + const timeDisplay = document.getElementById("timeRemaining"); + let minutes = Math.floor(time / 60) + .toString() + .padStart(2, "0"); + let seconds = (time % 60).toString().padStart(2, "0"); + timeDisplay.innerText = "Time Remaining: " + minutes + ":" + seconds; +} + +function resetAlarm() { + if (countdown !== null) { + clearInterval(countdown); + countdown = null; + } + + pauseAlarm(); + audio.currentTime = 0; + updateDisplay(0); +} + +function setAlarm() { + resetAlarm(); + + let secondsLeft = parseInt(document.getElementById("alarmSet").value, 10); + + if (isNaN(secondsLeft) || secondsLeft <= 0) { + updateDisplay(0); + return; + } + + updateDisplay(secondsLeft); + + countdown = setInterval(() => { + secondsLeft = secondsLeft - 1; + updateDisplay(secondsLeft); + + if (secondsLeft <= 0) { + clearInterval(countdown); + countdown = null; + playAlarm(); + } + }, 1000); +} // DO NOT EDIT BELOW HERE @@ -12,6 +56,13 @@ function setup() { document.getElementById("stop").addEventListener("click", () => { pauseAlarm(); }); + + document.getElementById("stop").addEventListener("click", () => { + if (countdown !== null) { + clearInterval(countdown); + countdown = null; + } + }); } function playAlarm() { @@ -22,4 +73,4 @@ function pauseAlarm() { audio.pause(); } -window.onload = setup; +window.onload = setup; \ No newline at end of file diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..ff2d3b453 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ -