Skip to content

Commit d0e8ded

Browse files
committed
Refactor setAlarm function to improve countdown timer logic and ensure immediate display update on button click
1 parent c1008ba commit d0e8ded

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Sprint-3/alarmclock/alarmclock.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ function setAlarm() {
1414
if (timerInterval) {
1515
clearInterval(timerInterval);
1616
}
17+
// Function to render the formatted time (MM:SS) to the DOM
18+
function updateDisplay() {
19+
const minutes = Math.floor(totalSeconds / 60);
20+
const seconds = totalSeconds % 60;
21+
22+
const formattedMinutes = String(minutes).padStart(2, "0");
23+
const formattedSeconds = String(seconds).padStart(2, "0");
24+
25+
timeRemainingEl.innerText = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
26+
}
27+
// 1. Immediately set heading on button click
28+
updateDisplay();
29+
30+
// 2. Start the countdown timer
31+
timerInterval = setInterval(() => {
32+
totalSeconds -= 1;
33+
34+
if (totalSeconds >= 0) {
35+
updateDisplay();
36+
}
37+
38+
if (totalSeconds === 0) {
39+
playAlarm();
40+
clearInterval(timerInterval);
41+
}
42+
}, 1000);
1743
}
1844

1945
// DO NOT EDIT BELOW HERE

0 commit comments

Comments
 (0)