-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathalarmclock.js
More file actions
61 lines (44 loc) · 1.26 KB
/
alarmclock.js
File metadata and controls
61 lines (44 loc) · 1.26 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
var audio = new Audio("alarmsound.mp3");
let timeRemaining = 0;
let countdownInterval = null;
function setAlarm() {
const input = document.getElementById("alarmSet").value;
timeRemaining = parseInt(input, 10);
if (isNaN(timeRemaining) || timeRemaining <= 0) {
alert("Please enter a valid number greater than 0");
return;
}
updateDisplay();
if (countdownInterval) {
clearInterval(countdownInterval);
}
countdownInterval = setInterval(() => {
timeRemaining--;
updateDisplay();
if (timeRemaining <= 0) {
clearInterval(countdownInterval);
playAlarm();
}
}, 1000);
}
function updateDisplay() {
const display = document.getElementById("timeRemaining");
const minutes = String(Math.floor(timeRemaining / 60)).padStart(2, "0");
const seconds = String(timeRemaining % 60).padStart(2, "0");
display.textContent = `Time Remaining: ${minutes}:${seconds}`;
}
function setup() {
document.getElementById("set").addEventListener("click", setAlarm);
document.getElementById("stop").addEventListener("click", pauseAlarm);
}
function playAlarm() {
audio.play();
}
function pauseAlarm() {
audio.pause();
audio.currentTime = 0;
if (countdownInterval) {
clearInterval(countdownInterval);
}
}
window.onload = setup;