|
1 | | -function setAlarm() {} |
| 1 | + |
2 | 2 | // Only define Audio if it doesn't exist (Node environment) |
3 | | -if (typeof Audio === "undefined") { |
4 | | - global.Audio = class { |
5 | | - constructor(src) { |
6 | | - this.src = src; |
| 3 | +if (typeof Audio === "undefined") { // In a Node define a mock Audio class to prevent errors when calling play() and pause() |
| 4 | + global.Audio = class { // Mock Audio class |
| 5 | + constructor(src) { // Takes a source for the audio file, but we won't load audio in this mock |
| 6 | + this.src = src; // stores the source (unused in the mock) |
7 | 7 | } |
8 | | - play() {} |
9 | | - pause() {} |
| 8 | + play() {} // no-op in this mock |
| 9 | + pause() {} // no-op in this mock |
10 | 10 | }; |
11 | 11 | } |
12 | 12 |
|
13 | | -let intervalId; |
| 13 | +let countdownIntervalId; //stores timer ID so we can clear it when needed |
14 | 14 |
|
15 | | -const formatTime = (s) => { |
16 | | - const mins = Math.floor(s / 60); |
17 | | - const secs = s % 60; |
18 | | - return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; |
| 15 | +const formatTime = (seconds) => { // takes a number of seconds and formats it into a string in the format "MM:SS" |
| 16 | + const minutes = Math.floor(seconds / 60); // calculates whole minutes |
| 17 | + const remainingSeconds = seconds % 60; // calculates remaining seconds |
| 18 | + return `${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; // formats as MM:SS |
19 | 19 | }; |
20 | 20 |
|
21 | | -function setAlarm() { |
22 | | - const input = document.getElementById("alarmSet"); |
23 | | - const timer = document.getElementById("timeRemaining"); |
24 | | - |
25 | | - const seconds = parseInt(input.value); |
26 | | - if (isNaN(seconds) || seconds < 0) return; |
| 21 | +function setAlarm() { // called "Set" button is clicked |
| 22 | + const alarmInput = document.getElementById("alarmSet"); // gets input for seconds |
| 23 | + const timerDisplay = document.getElementById("timeRemaining"); // gets element showing remaining time |
27 | 24 |
|
28 | | - let remaining = seconds; |
| 25 | + const seconds = parseInt(alarmInput.value, 10); // converts input value to integer |
| 26 | + if (isNaN(seconds) || seconds < 0) return; // if ignore invalid or negative input |
29 | 27 |
|
30 | | - // DO NOT EDIT BELOW HERE |
31 | | - timer.textContent = `Time Remaining: ${formatTime(remaining)}`; |
| 28 | + let remainingSeconds = seconds; // initializes countdown |
32 | 29 |
|
33 | | - if (intervalId) clearInterval(intervalId); |
| 30 | + timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // shows initial time |
34 | 31 |
|
35 | | - intervalId = setInterval(() => { |
36 | | - remaining--; |
| 32 | + if (countdownIntervalId) clearInterval(countdownIntervalId); // stops previous countdown to prevent multiple timers from running simultaneously |
37 | 33 |
|
38 | | - timer.textContent = `Time Remaining: ${formatTime(remaining)}`; |
| 34 | + countdownIntervalId = setInterval(() => { // timer runs every second |
| 35 | + remainingSeconds--; // decrements remaining time by 1 second |
39 | 36 |
|
40 | | - if (remaining <= 0) { |
41 | | - clearInterval(intervalId); |
42 | | - playAlarm(); |
| 37 | + if (remainingSeconds <= 0) { // when time runs out, clear the timer and play the alarm sound |
| 38 | + clearInterval(countdownIntervalId); // stops the countdown |
| 39 | + timerDisplay.textContent = "Time Remaining: 00:00"; // shows zero time |
| 40 | + playAlarm(); // plays alarm sound |
| 41 | + return; // prevents further execution |
43 | 42 | } |
44 | | - }, 1000); |
| 43 | + timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // updates countdown display |
| 44 | + }, 1000); // repeats every second |
45 | 45 | } |
46 | 46 |
|
47 | 47 | // DO NOT EDIT BELOW HERE |
48 | 48 |
|
49 | 49 | var audio = new Audio("alarmsound.mp3"); |
50 | 50 |
|
51 | | -function setup() { |
52 | | - document.getElementById("set").addEventListener("click", () => { |
53 | | - setAlarm(); |
| 51 | +function setup() { // This function sets up event listeners for the "Set" and "Stop" buttons when the page loads |
| 52 | + document.getElementById("set").addEventListener("click", () => { // adds a click event listener to the "Set" button that calls the setAlarm function when clicked |
| 53 | + setAlarm(); // calls the setAlarm function to start the timer when the "Set" button is clicked |
54 | 54 | }); |
55 | 55 |
|
56 | | - document.getElementById("stop").addEventListener("click", () => { |
57 | | - pauseAlarm(); |
| 56 | + document.getElementById("stop").addEventListener("click", () => { // adds a click event listener to the "Stop" button that calls the pauseAlarm function when clicked |
| 57 | + pauseAlarm(); // calls the pauseAlarm function to stop the alarm sound when the "Stop" button is clicked |
58 | 58 | }); |
59 | 59 | } |
60 | 60 |
|
61 | | -function playAlarm() { |
62 | | - audio.play(); |
| 61 | +function playAlarm() { // This function plays the alarm sound when called |
| 62 | + audio.play(); // calls the play method on the audio object to start playing the alarm sound |
63 | 63 | } |
64 | 64 |
|
65 | | -function pauseAlarm() { |
66 | | - audio.pause(); |
| 65 | +function pauseAlarm() { // This function pauses the alarm sound when called |
| 66 | + audio.pause(); // calls the pause method on the audio object to stop playing the alarm sound |
67 | 67 | } |
68 | 68 |
|
69 | | -window.onload = setup; |
| 69 | +window.onload = setup; // sets the setup function to run when the window finishes loading, ensuring that event listeners are set up properly |
0 commit comments