-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathalarmclock.js
More file actions
82 lines (65 loc) · 1.6 KB
/
alarmclock.js
File metadata and controls
82 lines (65 loc) · 1.6 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
let timeRemaining;
let timerInterval = null;
function formatTime(time) {
const minutes = String(Math.floor(time / 60)).padStart(2, "0");
const seconds = String(time % 60).padStart(2, "0");
return `${minutes}:${seconds}`;
}
function displayAlarm(time) {
const alarmBox = document.getElementById("timeRemaining");
alarmBox.textContent = `Time Remaining: ${formatTime(time)}`;
}
function decreaseAlarmTime() {
timeRemaining--;
displayAlarm(timeRemaining);
if (timeRemaining <= 0) {
clearInterval(timerInterval);
timerInterval = null;
playAlarm();
return;
}
}
function resetAlarm() {
clearInterval(timerInterval);
timerInterval = null;
pauseAlarm();
audio.currentTime = 0;
}
function setAlarm() {
const setTime = document.getElementById("alarmSet").value;
const numericTime = parseInt(setTime, 10);
if (isNaN(numericTime)) {
alert(
"Please enter your desired time in numbers, e.g., 120 for 2 minutes."
);
return;
}
if (numericTime < 0) {
alert("Please enter a non-negative number.");
return;
}
resetAlarm();
timeRemaining = numericTime;
displayAlarm(timeRemaining);
if (timeRemaining === 0) {
playAlarm();
} else {
timerInterval = setInterval(decreaseAlarmTime, 1000);
}
}
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;