-
-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathalarmclock.js
More file actions
118 lines (96 loc) · 4.78 KB
/
alarmclock.js
File metadata and controls
118 lines (96 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Only define Audio if it doesn't exist (Node environment)
if (typeof Audio === "undefined") {
// In a Node define a mock Audio class to prevent errors when calling play() and pause()
global.Audio = class {
// Mock Audio class
constructor(src) {
// Takes a source for the audio file, but we won't load audio in this mock
this.src = src; // stores the source (unused in the mock)
}
play() {} // no-op in this mock
pause() {} // no-op in this mock
};
}
let countdownIntervalId; //stores timer ID so we can clear it when needed
let alarmRunning = false; // tracks if a countdown is currently active
const formatTime = (seconds) => {
// takes a number of seconds and formats it into a string in the format "MM:SS"
const minutes = Math.floor(seconds / 60); // calculates whole minutes
const remainingSeconds = seconds % 60; // calculates remaining seconds
return `${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; // formats as MM:SS
};
function setAlarm() {
// called "Set" button is clicked
const alarmInput = document.getElementById("alarmSet"); // gets input for seconds
const timerDisplay = document.getElementById("timeRemaining"); // gets element showing remaining time
// Prevent starting a new alarm if one is already running
if (alarmRunning) {
alert("Please press Stop before setting a new alarm."); // ensues user cannot set another alarm without stopping the current one first
return;
}
const seconds = parseInt(alarmInput.value, 10); // converts input value to integer
if (isNaN(seconds) || seconds < 0) return; // if ignore invalid or negative input
let remainingSeconds = seconds; // initializes countdown
timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // shows initial time
if (countdownIntervalId) {
clearInterval(countdownIntervalId); // stops previous countdown to prevent multiple timers from running simultaneously
countdownIntervalId = null; // resets timer ID
}
if (remainingSeconds === 0) {
// if the input is zero, play the alarm immediately without starting a countdown
playAlarm();
return;
}
alarmRunning = true; // mark that alarm is active
countdownIntervalId = setInterval(() => {
remainingSeconds--; // decrements remaining time by 1 second
if (remainingSeconds <= 0) {
// when time runs out, clear the timer and play the alarm sound
clearInterval(countdownIntervalId); // stops the countdown timer
countdownIntervalId = null; // resets timer ID
alarmRunning = false; // mark that alarm is no longer active
timerDisplay.textContent = "Time Remaining: 00:00"; // shows zero time
playAlarm(); // plays alarm sound
return; // prevents further execution
}
timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // updates countdown display
}, 1000); // repeats every second
}
window.addEventListener("load", () => {
// Add a listener to the Stop button that also clears the timer
const stopBtn = document.getElementById("stop");
stopBtn.addEventListener("click", () => {
// stops countdown amd stops audio
if (countdownIntervalId) {
clearInterval(countdownIntervalId);
countdownIntervalId = null;
}
alarmRunning = false; // mark that alarm is no longer active
document.getElementById("timeRemaining").textContent =
"Time Remaining: 00:00"; // resets display when stopped
document.getElementById("alarmSet").value = ""; // clears input field when stopped
pauseAlarm(); // stop audio if it's playing
});
});
// DO NOT EDIT BELOW HERE
var audio = new Audio("alarmsound.mp3");
function setup() {
// This function sets up event listeners for the "Set" and "Stop" buttons when the page loads
document.getElementById("set").addEventListener("click", () => {
// adds a click event listener to the "Set" button that calls the setAlarm function when clicked
setAlarm(); // calls the setAlarm function to start the timer when the "Set" button is clicked
});
document.getElementById("stop").addEventListener("click", () => {
// adds a click event listener to the "Stop" button that calls the pauseAlarm function when clicked
pauseAlarm(); // calls the pauseAlarm function to stop the alarm sound when the "Stop" button is clicked
});
}
function playAlarm() {
// This function plays the alarm sound when called
audio.play(); // calls the play method on the audio object to start playing the alarm sound
}
function pauseAlarm() {
// This function pauses the alarm sound when called
audio.pause(); // calls the pause method on the audio object to stop playing the alarm sound
}
window.onload = setup; // sets the setup function to run when the window finishes loading, ensuring that event listeners are set up properly