Skip to content

Commit 16a4347

Browse files
committed
audio reset cleaned up
1 parent 0df1cfb commit 16a4347

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
let countdownId;
22

3-
// moved to outer scope, takes seconds as a parameter
4-
function updateDisplay(seconds) {
5-
const minutes = Math.floor(seconds / 60);
6-
const remainingSeconds = seconds % 60;
7-
document.getElementById("timeRemaining").textContent =
8-
`Time Remaining: ${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`;
9-
}
3+
// moved to outer scope, takes seconds as a parameter
4+
function updateDisplay(seconds) {
5+
const minutes = Math.floor(seconds / 60);
6+
const remainingSeconds = seconds % 60;
7+
document.getElementById("timeRemaining").textContent =
8+
`Time Remaining: ${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`;
9+
}
1010

1111
// reset before starting new countdown
1212
function resetAlarm() {
1313
clearInterval(countdownId);
14-
updateDisplay(0); // replaces the manual textContent line
14+
audio.pause();
15+
audio.currentTime = 0;
16+
updateDisplay(0);
1517
document.body.classList.toggle("alarm-activated", false);
1618
}
1719

1820
function setAlarm() {
21+
resetAlarm();
1922
let seconds = parseInt(document.getElementById("alarmSet").value);
2023

2124
if (!seconds || seconds < 1) {
2225
alert("The number of seconds must be higher than 0 please");
2326
return;
2427
}
2528

26-
updateDisplay(seconds);
27-
// pass seconds as argument and update immediately on click
29+
updateDisplay(seconds);
30+
// pass seconds as argument and update immediately on click
2831

2932
countdownId = setInterval(() => {
3033
seconds--;
@@ -57,4 +60,8 @@ function playAlarm() {
5760
audio.play();
5861
}
5962

63+
function pauseAlarm() {
64+
audio.pause();
65+
}
66+
6067
window.onload = setup;

0 commit comments

Comments
 (0)