|
1 | 1 | function setAlarm() {} |
| 2 | +const display = document.querySelector("#timeRemaining"); |
| 3 | +const totalSeconds = document.querySelector("#alarmSet"); |
| 4 | +const setButton = document.querySelector("#set"); |
| 5 | +const stopButton = document.querySelector("#stop"); |
| 6 | +let conuntdownInterval; |
| 7 | + |
| 8 | +// time management function |
| 9 | +function formatTime(totalSeconds) { |
| 10 | + const minutes = Math.floor(totalSeconds / 60); |
| 11 | + const seconds = totalSeconds % 60; |
| 12 | + |
| 13 | + const paddedMinutes = String(minutes).padStart(2, "0"); |
| 14 | + const paddedSeconds = String(seconds).padStart(2, "0"); |
| 15 | + return `Time Remaining: ${paddedMinutes}:${paddedSeconds}`; |
| 16 | +} |
| 17 | + |
| 18 | +// to clear every change made with conditions |
| 19 | +function clearEverything() { |
| 20 | + display.style.color = "black"; |
| 21 | + setButton.disabled = false; |
| 22 | + document.body.style.background = ""; |
| 23 | +} |
| 24 | + |
| 25 | +// button event handler |
| 26 | +setButton.addEventListener("click", () => { |
| 27 | + let timeleft = Number(totalSeconds.value); |
| 28 | + setButton.disabled = true; |
| 29 | + |
| 30 | + // time 0 |
| 31 | + |
| 32 | + if (timeleft <= 0) { |
| 33 | + alert("please enter a number greater than 0!"); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + display.textContent = formatTime(timeleft); |
| 38 | + |
| 39 | + conuntdownInterval = setInterval(() => { |
| 40 | + if (timeleft == 0) { |
| 41 | + clearInterval(conuntdownInterval); |
| 42 | + document.body.style.backgroundColor = "grey"; |
| 43 | + playAlarm(); |
| 44 | + } |
| 45 | + |
| 46 | + if (timeleft <= 10) { |
| 47 | + display.style.color = "red"; |
| 48 | + } |
| 49 | + |
| 50 | + if (timeleft >= 0) { |
| 51 | + display.textContent = formatTime(timeleft); |
| 52 | + timeleft--; |
| 53 | + } |
| 54 | + }, 1000); |
| 55 | +}); |
| 56 | + |
| 57 | +//stop button clicked |
| 58 | +stopButton.addEventListener("click", () => { |
| 59 | + clearInterval(conuntdownInterval); |
| 60 | + clearEverything(); |
| 61 | + pauseAlarm(); |
| 62 | +}); |
2 | 63 |
|
3 | 64 | // DO NOT EDIT BELOW HERE |
4 | 65 |
|
|
0 commit comments