Skip to content

Commit 7f63885

Browse files
committed
Build alarm clock countdown functionality with sound and stop feature
1 parent 96d077b commit 7f63885

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

Sprint-3/alarmclock/alarmclock.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
1-
function setAlarm() {}
1+
function setAlarm() {
2+
const input = document.getElementById("alarmSet").value;
3+
4+
if (!input || input <= 0) {
5+
alert("Please enter a valid number");
6+
return;
7+
}
8+
9+
let timeRemaining = parseInt(input);
10+
const display = document.getElementById("timeRemaining");
11+
12+
// Show initial time
13+
display.textContent = `Time Remaining: 00:${timeRemaining
14+
.toString()
15+
.padStart(2, "0")}`;
16+
17+
// Clear any previous timer
18+
clearInterval(countdown);
19+
20+
countdown = setInterval(() => {
21+
timeRemaining--;
22+
23+
display.textContent = `Time Remaining: 00:${timeRemaining
24+
.toString()
25+
.padStart(2, "0")}`;
26+
27+
if (timeRemaining === 0) {
28+
clearInterval(countdown);
29+
playAlarm();
30+
}
31+
}, 1000);
32+
}
233

334
// DO NOT EDIT BELOW HERE
435

36+
let countdown; // 👈 needed for timer control
37+
538
var audio = new Audio("alarmsound.mp3");
639

740
function setup() {
@@ -20,6 +53,7 @@ function playAlarm() {
2053

2154
function pauseAlarm() {
2255
audio.pause();
56+
clearInterval(countdown);
2357
}
2458

25-
window.onload = setup;
59+
window.onload = setup;

0 commit comments

Comments
 (0)