-
-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathalarmclock.js
More file actions
75 lines (60 loc) · 1.95 KB
/
alarmclock.js
File metadata and controls
75 lines (60 loc) · 1.95 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
let timerInterval = null;
let remainingSeconds = 0;
function setAlarm() {
// Read the minutes value from the alarm input field
const minutesInput = document.getElementById("alarmSet");
const minutes = parseInt(minutesInput.value, 10);
// Ignore invalid or non-positive input
if (isNaN(minutes) || minutes <= 0) {
return;
}
// Store the input as the remaining seconds to count down
remainingSeconds = minutes * 60;
// Clear any existing timer before starting a new one
if (timerInterval !== null) {
clearInterval(timerInterval);
timerInterval = null;
}
// Immediately render the starting time on screen
updateTimeDisplay();
// Tick every second and decrement remainingSeconds
timerInterval = setInterval(() => {
remainingSeconds = remainingSeconds - 1;
updateTimeDisplay();
if (remainingSeconds <= 0) {
// Countdown finished — stop the timer and trigger the alarm
clearInterval(timerInterval);
timerInterval = null;
remainingSeconds = 0;
// Ensure the display shows exactly 00:00
updateTimeDisplay();
playAlarm();
}
}, 1000);
}
// Converts remainingSeconds into MM:SS format and updates the display
function updateTimeDisplay() {
const minutes = Math.floor(remainingSeconds / 60);
const seconds = remainingSeconds % 60;
const formattedMinutes = minutes.toString().padStart(2, "0");
const formattedSeconds = seconds.toString().padStart(2, "0");
const timeDisplay = document.getElementById("timeRemaining");
timeDisplay.textContent = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
}
// 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;