Skip to content

Commit 6fee600

Browse files
author
Ogbemi mene
committed
my alarm app
1 parent 4222cda commit 6fee600

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
1-
function setAlarm() {}
1+
let countdownInterval = null;
2+
function setAlarm() {
3+
const input = document.getElementById("alarmSet");
4+
let totalSeconds = parseInt(input.value, 10);
25

6+
if (isNaN(totalSeconds) || totalSeconds <= 0) {
7+
return;
8+
}
9+
10+
if (countdownInterval) {
11+
clearInterval(countdownInterval);
12+
}
13+
14+
const heading = document.getElementById("timeRemaining");
15+
16+
function updateDisplay(seconds) {
17+
const minutes = Math.floor(seconds / 60);
18+
const remSeconds = seconds % 60;
19+
20+
const formattedMinutes = String(minutes).padStart(2, "0");
21+
const formattedSeconds = String(remSeconds).padStart(2, "0");
22+
23+
heading.textContent = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
24+
}
25+
26+
// Set initial display immediately when button is pressed
27+
updateDisplay(totalSeconds);
28+
29+
// Start 1000ms interval countdown
30+
countdownInterval = setInterval(() => {
31+
totalSeconds--;
32+
updateDisplay(totalSeconds);
33+
34+
if (totalSeconds <= 0) {
35+
clearInterval(countdownInterval);
36+
playAlarm();
37+
}
38+
}, 1000);
39+
}
340
// DO NOT EDIT BELOW HERE
441

542
var audio = new Audio("alarmsound.mp3");

0 commit comments

Comments
 (0)