-
-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathalarmclock.js
More file actions
60 lines (47 loc) · 1.46 KB
/
alarmclock.js
File metadata and controls
60 lines (47 loc) · 1.46 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
let countdownId;
// moved to outer scope, takes seconds as a parameter
function updateDisplay(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
document.getElementById("timeRemaining").textContent =
`Time Remaining: ${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`;
}
// reset before starting new countdown
function resetAlarm() {
clearInterval(countdownId);
updateDisplay(0); // replaces the manual textContent line
document.body.classList.toggle("alarm-activated", false);
}
function setAlarm() {
let seconds = parseInt(document.getElementById("alarmSet").value);
if (!seconds || seconds < 1) {
alert("The number of seconds must be higher than 0 please");
return;
}
updateDisplay(seconds);
// pass seconds as argument and update immediately on click
countdownId = setInterval(() => {
seconds--;
updateDisplay(seconds);
// pass seconds as argument
if (seconds <= 0) {
clearInterval(countdownId);
playAlarm();
document.body.classList.toggle("alarm-activated", true);
}
}, 1000);
}
// DO NOT EDIT BELOW HERE
var audio = new Audio("alarmsound.mp3");
function setup() {
document.getElementById("set").addEventListener("click", () => {
setAlarm();
});
document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
});
}
function playAlarm() {
audio.play();
}
window.onload = setup;