Skip to content

Commit 2a57b48

Browse files
author
JorvanW
committed
udpated code so alarm sound also resets when the user presses set alarm after the sound is going
1 parent 7f10e87 commit 2a57b48

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@ let timer;
33
let seconds;
44

55
function setAlarm() {
6-
const alarmSetInput = document.getElementById("alarmSet");
7-
seconds = Number(alarmSetInput.value);
6+
const alarmSetInput = document.getElementById("alarmSet");
7+
seconds = Number(alarmSetInput.value);
88

9+
if (seconds <= 0 || isNaN(seconds)) {
10+
alert("Please enter a valid number.");
11+
return;
12+
}
913

10-
if (seconds <= 0 || isNaN(seconds)) {
11-
alert("Please enter a valid number.");
12-
return;
13-
}
14+
// Stop a previous countdown if there is one
15+
clearInterval(timer);
16+
pauseAlarm();
1417

15-
// Stop a previous countdown if there is one
16-
clearInterval(timer);
18+
// Run immediately
19+
decrementTimer();
1720

18-
// Run immediately
19-
decrementTimer();
20-
21-
// decrement each second
22-
timer = setInterval(() => decrementTimer(), 1000);
21+
// decrement each second
22+
timer = setInterval(() => decrementTimer(), 1000);
2323
}
2424

25-
function decrementTimer()
26-
{
25+
function decrementTimer() {
2726
const minutes = Math.floor(seconds / 60);
2827
const secs = seconds % 60;
2928

@@ -61,4 +60,3 @@ function pauseAlarm() {
6160
}
6261

6362
window.onload = setup;
64-

0 commit comments

Comments
 (0)