-
-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathalarmclock.js
More file actions
58 lines (44 loc) · 1.24 KB
/
alarmclock.js
File metadata and controls
58 lines (44 loc) · 1.24 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
let countdown = null;
function setAlarm() {
let secondsLeft = parseInt(document.getElementById("alarmSet").value, 10);
const timeDisplay = document.getElementById("timeRemaining");
if (isNaN(secondsLeft) || secondsLeft <= 0) {
timeDisplay.innerText = "Time Remaining: 00:00";
return;
}
if (countdown !== null) {
clearInterval(countdown);
}
const updateDisplay = (time) => {
let minutes = Math.floor(time / 60).toString().padStart(2, "0");
let seconds = (time % 60).toString().padStart(2, "0");
timeDisplay.innerText = "Time Remaining: " + minutes + ":" + seconds;
};
updateDisplay(secondsLeft);
countdown = setInterval(() => {
secondsLeft = secondsLeft - 1;
updateDisplay(secondsLeft);
if (secondsLeft <= 0) {
clearInterval(countdown);
countdown = null;
playAlarm();
}
}, 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();
}
function pauseAlarm() {
audio.pause();
}
window.onload = setup;