Skip to content

Commit 0c30fdd

Browse files
committed
modifications are made when timer is set to 0
1 parent fc92b97 commit 0c30fdd

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,31 @@ function setAlarm() {
4343

4444
// Clear previous countdown
4545
function resetAlarmState() {
46-
clearInterval(timer);
47-
clearInterval(flashing);
48-
flashing = null;
49-
document.body.style.backgroundColor = "";
50-
pauseAlarm();
51-
audio.currentTime = 0;
52-
}
53-
54-
resetAlarmState();
46+
clearInterval(timer);
47+
clearInterval(flashing);
48+
flashing = null;
49+
document.body.style.backgroundColor = "";
50+
pauseAlarm();
51+
audio.currentTime = 0;
52+
}
5553

54+
resetAlarmState();
5655

57-
function tick() {
58-
if (timeLeft > 0) {
56+
if (timeLeft === 0) {
57+
// Special case: alarm should start immediately
58+
startAlarm();
59+
} else {
60+
timer = setInterval(() => {
5961
timeLeft--;
6062
updateDisplay(timeLeft);
61-
} else {
62-
clearInterval(timer);
63-
startAlarm();
64-
}
65-
}
6663

67-
// Run once immediately for consistency
68-
tick();
69-
timer = setInterval(tick, 1000);
64+
if (timeLeft === 0) {
65+
clearInterval(timer);
66+
startAlarm();
67+
}
68+
}, 1000);
69+
}
7070
}
71-
7271
function updateDisplay(seconds) {
7372
const mins = String(Math.floor(seconds / 60)).padStart(2, "0");
7473
const secs = String(seconds % 60).padStart(2, "0");

0 commit comments

Comments
 (0)