|
1 | | -function displayTime(time) { |
2 | | - const remainingElement = document.getElementById("timeRemaining"); |
3 | | - |
4 | | - const minutes = String(Math.floor(time / 60)).padStart(2, "0"); |
5 | | - const seconds = String(time % 60).padStart(2, "0"); |
| 1 | +let interval; |
6 | 2 |
|
7 | | - remainingElement.innerText = `Time Remaining: ${minutes}:${seconds}`; |
| 3 | +function displayTime(time) { |
| 4 | + const remainingElement = document.getElementById("timeRemaining"); |
| 5 | + const minutes = String(Math.floor(time / 60)).padStart(2, "0"); |
| 6 | + const seconds = String(time % 60).padStart(2, "0"); |
| 7 | + remainingElement.innerText = `Time Remaining: ${minutes}:${seconds}`; |
8 | 8 | } |
9 | 9 |
|
10 | 10 | function setAlarm() { |
11 | | - let time = parseInt(document.getElementById("alarmSet").value); |
12 | | - displayTime(time); |
| 11 | + let time = Number(document.getElementById("alarmSet").value); |
| 12 | + |
| 13 | + if (isNaN(time) || time <= 0) { |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + clearInterval(interval); |
| 18 | + pauseAlarm(); |
13 | 19 |
|
14 | | - const interval = setInterval(() => { |
15 | | - time--; |
16 | | - displayTime(time); |
17 | | - if (time === 0) { |
18 | | - clearInterval(interval); |
19 | | - playAlarm(); |
| 20 | + if (!Number.isInteger(time) || time <= 0) { |
| 21 | + return; |
20 | 22 | } |
21 | | - }, 1000); |
22 | | -} |
23 | 23 |
|
| 24 | + displayTime(time); |
24 | 25 |
|
| 26 | + interval = setInterval(() => { |
| 27 | + time--; |
| 28 | + displayTime(time); |
25 | 29 |
|
| 30 | + if (time === 0) { |
| 31 | + clearInterval(interval); |
| 32 | + playAlarm(); |
| 33 | + } |
| 34 | + }, 1000); |
| 35 | +} |
26 | 36 |
|
| 37 | +window.addEventListener("load", () => { |
| 38 | + document.getElementById("stop").addEventListener("click", () => { |
| 39 | + clearInterval(interval); |
| 40 | + }); |
| 41 | +}); |
27 | 42 |
|
28 | 43 |
|
29 | 44 | // DO NOT EDIT BELOW HERE |
|
0 commit comments