|
1 | 1 | let countdownId; |
2 | 2 |
|
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 | +} |
10 | 10 |
|
11 | 11 | // reset before starting new countdown |
12 | 12 | function resetAlarm() { |
13 | 13 | clearInterval(countdownId); |
14 | | - updateDisplay(0); // replaces the manual textContent line |
| 14 | + audio.pause(); |
| 15 | + audio.currentTime = 0; |
| 16 | + updateDisplay(0); |
15 | 17 | document.body.classList.toggle("alarm-activated", false); |
16 | 18 | } |
17 | 19 |
|
18 | 20 | function setAlarm() { |
| 21 | + resetAlarm(); |
19 | 22 | let seconds = parseInt(document.getElementById("alarmSet").value); |
20 | 23 |
|
21 | 24 | if (!seconds || seconds < 1) { |
22 | 25 | alert("The number of seconds must be higher than 0 please"); |
23 | 26 | return; |
24 | 27 | } |
25 | 28 |
|
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 |
28 | 31 |
|
29 | 32 | countdownId = setInterval(() => { |
30 | 33 | seconds--; |
@@ -57,4 +60,8 @@ function playAlarm() { |
57 | 60 | audio.play(); |
58 | 61 | } |
59 | 62 |
|
| 63 | +function pauseAlarm() { |
| 64 | + audio.pause(); |
| 65 | +} |
| 66 | + |
60 | 67 | window.onload = setup; |
0 commit comments