|
1 | | -// This variable will store the interval so we can stop it later |
2 | | -let flashInterval; |
| 1 | +// This is the countdown number that will go down every second |
| 2 | +let countdownTimer; |
3 | 3 |
|
4 | | -// This function makes the background flash red and white |
5 | 4 | function startFlashing() { |
6 | | - let isRed = false; // keeps track of which color we should show |
7 | | - |
8 | | - flashInterval = setInterval(function () { |
9 | | - if (isRed) { |
10 | | - document.body.style.backgroundColor = "white"; |
11 | | - } else { |
12 | | - document.body.style.backgroundColor = "red"; |
13 | | - } |
14 | | - |
15 | | - // Switch the color for next time |
16 | | - isRed = !isRed; |
17 | | - }, 500); // run every half second |
| 5 | + document.body.classList.add("flash-background"); |
18 | 6 | } |
19 | 7 |
|
20 | | -// This function stops the flashing and resets the background |
21 | 8 | function stopFlashing() { |
22 | | - clearInterval(flashInterval); // stop the flashing interval |
23 | | - document.body.style.backgroundColor = "white"; // reset background |
| 9 | + document.body.classList.remove("flash-background"); |
24 | 10 | } |
25 | 11 |
|
26 | 12 | function setAlarm() { |
27 | | - // get the value of the input |
28 | | - const input = document.getElementById("alarmSet").value; |
| 13 | + const rawInput = document.getElementById("alarmSet").value.trim(); |
29 | 14 |
|
30 | | - // If nothing was typed, exit the function |
31 | | - if (!input) { |
| 15 | + // Reject empty input |
| 16 | + if (rawInput === "") { |
| 17 | + alert("Please enter a time."); |
32 | 18 | return; |
33 | 19 | } |
34 | 20 |
|
35 | | - let totalSeconds; |
| 21 | + // 2. Parse input → returns either a number of seconds or null |
| 22 | + const totalSeconds = parseInput(rawInput); |
36 | 23 |
|
37 | | - // If the user typed something like "2:21" |
38 | | - if (input.includes(":")) { |
39 | | - // Split into minutes and seconds |
40 | | - const parts = input.split(":"); |
41 | | - const minutes = Number(parts[0]); |
42 | | - const seconds = Number(parts[1]); |
| 24 | + // 3. Reject invalid parsed values |
| 25 | + if (totalSeconds === null || totalSeconds <= 0) { |
| 26 | + alert("Please enter a valid positive time."); |
| 27 | + return; |
| 28 | + } |
43 | 29 |
|
44 | | - // Convert everything into total seconds |
45 | | - totalSeconds = minutes * 60 + seconds; |
46 | | - } else { |
47 | | - // Otherwise treat the input as normal seconds |
48 | | - totalSeconds = Number(input); |
| 30 | + // 4. Reject extremely large values |
| 31 | + if (totalSeconds > 36000) { |
| 32 | + alert("Please enter a time less than 10 hours."); |
| 33 | + return; |
49 | 34 | } |
50 | 35 |
|
51 | | - // This is the countdown number that will go down every second |
52 | | - let timeLeft = totalSeconds; |
| 36 | + stopFlashing(); |
| 37 | + pauseAlarm(); |
| 38 | + |
| 39 | + // 5. If we reach here → input is valid |
| 40 | + startCountdown(totalSeconds); |
| 41 | +} |
| 42 | + |
| 43 | +function parseInput(rawInput) { |
| 44 | + if (rawInput.includes(":")) { |
| 45 | + const [minsStr, secsStr] = rawInput.split(":"); |
| 46 | + |
| 47 | + const mins = Number(minsStr); |
| 48 | + const secs = Number(secsStr); |
| 49 | + |
| 50 | + if ( |
| 51 | + Number.isNaN(mins) || |
| 52 | + Number.isNaN(secs) || |
| 53 | + mins < 0 || |
| 54 | + secs < 0 || |
| 55 | + secs >= 60 |
| 56 | + ) { |
| 57 | + return null; |
| 58 | + } |
53 | 59 |
|
54 | | - // If a previous timer was running, stop it |
55 | | - if (window.countdownTimer) { |
56 | | - clearInterval(window.countdownTimer); |
| 60 | + return mins * 60 + secs; |
57 | 61 | } |
58 | 62 |
|
59 | | - // A simple helper to turn seconds into MM:SS format |
60 | | - function formatTime(seconds) { |
61 | | - const mins = Math.floor(seconds / 60); |
62 | | - const secs = seconds % 60; |
| 63 | + const secs = Number(rawInput); |
| 64 | + return Number.isNaN(secs) ? null : secs; |
| 65 | +} |
63 | 66 |
|
64 | | - // Make sure both numbers always have two digits |
65 | | - const paddedMins = String(mins).padStart(2, "0"); |
66 | | - const paddedSecs = String(secs).padStart(2, "0"); |
| 67 | +function startCountdown(totalSeconds) { |
| 68 | + let timeLeft = totalSeconds; |
67 | 69 |
|
68 | | - return `${paddedMins}:${paddedSecs}`; |
| 70 | + if (countdownTimer) { |
| 71 | + clearInterval(countdownTimer); |
69 | 72 | } |
70 | 73 |
|
71 | | - // Show the starting time immediately |
72 | | - document.getElementById("timeRemaining").innerText = |
73 | | - `Time Remaining: ${formatTime(timeLeft)}`; |
74 | | - |
75 | | - // Start the countdown — runs every 1000ms (1 second) |
76 | | - window.countdownTimer = setInterval(() => { |
77 | | - timeLeft--; // reduce by 1 second |
| 74 | + updateDisplay(timeLeft); |
78 | 75 |
|
79 | | - // Update the heading each second |
80 | | - document.getElementById("timeRemaining").innerText = |
81 | | - `Time Remaining: ${formatTime(timeLeft)}`; |
| 76 | + countdownTimer = setInterval(() => { |
| 77 | + timeLeft--; |
| 78 | + updateDisplay(timeLeft); |
82 | 79 |
|
83 | | - // When the timer reaches zero |
84 | 80 | if (timeLeft <= 0) { |
85 | | - clearInterval(window.countdownTimer); // stop the countdown |
86 | | - clearInterval(flashInterval); // stop the flashing if it was running |
87 | | - document.getElementById("timeRemaining").innerText = |
88 | | - `Time Remaining: 00:00`; |
89 | | - playAlarm(); // play the alarm sound |
90 | | - startFlashing(); // start flashing the background |
| 81 | + clearInterval(countdownTimer); |
| 82 | + playAlarm(); |
| 83 | + startFlashing(); |
91 | 84 | } |
92 | 85 | }, 1000); |
93 | 86 | } |
94 | 87 |
|
| 88 | +// A simple helper to turn seconds into MM:SS format |
| 89 | +function formatTime(seconds) { |
| 90 | + const mins = Math.floor(seconds / 60); |
| 91 | + const secs = seconds % 60; |
| 92 | + |
| 93 | + // Make sure both numbers always have two digits |
| 94 | + const paddedMins = String(mins).padStart(2, "0"); |
| 95 | + const paddedSecs = String(secs).padStart(2, "0"); |
| 96 | + |
| 97 | + return `${paddedMins}:${paddedSecs}`; |
| 98 | +} |
| 99 | + |
| 100 | +function updateDisplay(seconds) { |
| 101 | + document.getElementById("timeRemaining").innerText = |
| 102 | + `Time Remaining: ${formatTime(seconds)}`; |
| 103 | +} |
| 104 | + |
95 | 105 | // DO NOT EDIT BELOW HERE |
96 | 106 |
|
97 | 107 | var audio = new Audio("alarmsound.mp3"); |
|
0 commit comments