|
1 | | -function setAlarm() {} |
| 1 | +// Store the current countdown value in seconds |
| 2 | +let remainingSeconds = 0; |
| 3 | + |
| 4 | +// Store the timer ID so an existing timer can be cleared |
| 5 | +let countdownTimerId = null; |
| 6 | + |
| 7 | +/** |
| 8 | + * Converts a number of seconds into mm:ss format. |
| 9 | + * Example: |
| 10 | + * 19 -> 00:19 |
| 11 | + * 119 -> 01:59 |
| 12 | + */ |
| 13 | +function formatTime(totalSeconds) { |
| 14 | + const minutes = Math.floor(totalSeconds / 60); |
| 15 | + const seconds = totalSeconds % 60; |
| 16 | + |
| 17 | + const formattedMinutes = String(minutes).padStart(2, "0"); |
| 18 | + const formattedSeconds = String(seconds).padStart(2, "0"); |
| 19 | + |
| 20 | + return `${formattedMinutes}:${formattedSeconds}`; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Updates the heading text with the current remaining time. |
| 25 | + */ |
| 26 | +function updateHeading() { |
| 27 | + const heading = document.getElementById("timeRemaining"); |
| 28 | + heading.innerText = `Time Remaining: ${formatTime(remainingSeconds)}`; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Resets the background colour to the default state. |
| 33 | + */ |
| 34 | +function resetBackground() { |
| 35 | + document.body.classList.remove("alarm-finished"); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Changes the background colour when the timer reaches zero. |
| 40 | + */ |
| 41 | +function showAlarmFinishedState() { |
| 42 | + document.body.classList.add("alarm-finished"); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Stops any existing countdown timer. |
| 47 | + */ |
| 48 | +function stopCountdown() { |
| 49 | + if (countdownTimerId !== null) { |
| 50 | + clearInterval(countdownTimerId); |
| 51 | + countdownTimerId = null; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Starts the countdown timer and updates the heading every second. |
| 57 | + */ |
| 58 | +function startCountdown() { |
| 59 | + countdownTimerId = setInterval(() => { |
| 60 | + if (remainingSeconds > 0) { |
| 61 | + remainingSeconds -= 1; |
| 62 | + updateHeading(); |
| 63 | + } |
| 64 | + |
| 65 | + if (remainingSeconds === 0) { |
| 66 | + stopCountdown(); |
| 67 | + showAlarmFinishedState(); |
| 68 | + playAlarm(); |
| 69 | + } |
| 70 | + }, 1000); |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Reads the value from the input, updates the heading, |
| 75 | + * resets the background, stops any existing alarm sound, |
| 76 | + * and starts the countdown. |
| 77 | + */ |
| 78 | +function setAlarm() { |
| 79 | + const input = document.getElementById("alarmSet"); |
| 80 | + const inputValue = Number(input.value); |
| 81 | + |
| 82 | + // Prevent invalid negative values |
| 83 | + if (Number.isNaN(inputValue) || inputValue < 0) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // Stop any previous countdown before starting a new one |
| 88 | + stopCountdown(); |
| 89 | + |
| 90 | + // Stop any alarm sound that may already be playing |
| 91 | + pauseAlarm(); |
| 92 | + |
| 93 | + // Reset the page state for a new alarm |
| 94 | + resetBackground(); |
| 95 | + |
| 96 | + // Store the new number of seconds and update the heading immediately |
| 97 | + remainingSeconds = Math.floor(inputValue); |
| 98 | + updateHeading(); |
| 99 | + |
| 100 | + // Start counting down every second |
| 101 | + startCountdown(); |
| 102 | +} |
2 | 103 |
|
3 | 104 | // DO NOT EDIT BELOW HERE |
4 | 105 |
|
5 | 106 | var audio = new Audio("alarmsound.mp3"); |
6 | 107 |
|
7 | 108 | function setup() { |
| 109 | + // Make the alarm loop continuously until stopped |
| 110 | + audio.loop = true; |
| 111 | + |
8 | 112 | document.getElementById("set").addEventListener("click", () => { |
9 | 113 | setAlarm(); |
10 | 114 | }); |
|
0 commit comments