-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathalarmclock.js
More file actions
81 lines (62 loc) · 1.75 KB
/
alarmclock.js
File metadata and controls
81 lines (62 loc) · 1.75 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
let interval;
let timeRemaining = 0;
let flashInterval;
function setAlarm() {
clearInterval(interval);
clearInterval(flashInterval);
const input = document.getElementById("alarmSet");
const display = document.getElementById("timeRemaining");
timeRemaining = parseInt(input.value);
if (isNaN(timeRemaining) || timeRemaining <= 0) return;
updateDisplay();
interval = setInterval(() => {
if (timeRemaining <= 0) {
clearInterval(interval);
timeRemaining = 0;
updateDisplay();
playAlarm();
// Start flashing background
flashInterval = setInterval(() => {
document.body.style.backgroundColor =
document.body.style.backgroundColor === "green" ? "white" : "green";
}, 500);
const stopButton = document.getElementById("stop");
stopButton.addEventListener(
"click",
() => {
clearInterval(flashInterval);
document.body.style.backgroundColor = "white";
},
{ once: true }
);
return;
}
// Decrement only if above 0
timeRemaining--;
updateDisplay();
}, 1000);
function updateDisplay() {
const minutes = Math.floor(timeRemaining / 60);
const seconds = timeRemaining % 60;
const mm = String(minutes).padStart(2, "0");
const ss = String(seconds).padStart(2, "0");
display.textContent = `Time Remaining: ${mm}:${ss}`;
}
}
// 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;