Skip to content

Commit a296952

Browse files
input box cleared after set alarm runs
1 parent 0161e64 commit a296952

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,37 @@
77
// When the `Time Remaining` reaches `00:00` the alarm should play a sound. You can make the sound happen by using `playAlarm()`.
88

99
// You can stop the alarm sound by pressing the `Stop Alarm` button.
10-
11-
//
1210
const alarmInput = document.querySelector("#alarmSet");
13-
const setButton = document.querySelector("#set");
1411
const timeRemaining = document.querySelector("#timeRemaining");
12+
13+
let timer;
14+
1515
function setAlarm() {
16+
clearInterval(timer);
17+
pauseAlarm();
1618
let totalSeconds = Number(alarmInput.value);
17-
const timer = setInterval(() => {
18-
totalSeconds--;
19+
alarmInput.value = "";
20+
if (totalSeconds <= 0 || !Number.isFinite(totalSeconds)) {
21+
timeRemaining.innerText = "Time Remaining: 00:00";
22+
return;
23+
}
24+
function updateTime() {
1925
const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0");
20-
const seconds = String(Math.floor(totalSeconds % 60)).padStart(2, "0");
21-
const remainingTime = minutes + ":" + seconds;
22-
timeRemaining.innerText = `Time Remaining:${remainingTime}`;
26+
const seconds = String(totalSeconds % 60).padStart(2, "0");
27+
28+
timeRemaining.innerText = `Time Remaining: ${minutes}:${seconds}`;
29+
}
30+
updateTime();
31+
timer = setInterval(() => {
32+
totalSeconds--;
33+
updateTime();
34+
2335
if (totalSeconds === 0) {
2436
clearInterval(timer);
2537
playAlarm();
2638
}
2739
}, 1000);
2840
}
29-
30-
setButton.addEventListener("click", setAlarm);
3141
// DO NOT EDIT BELOW HERE
3242

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

0 commit comments

Comments
 (0)