-
-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathalarmclock.js
More file actions
49 lines (38 loc) · 1.13 KB
/
alarmclock.js
File metadata and controls
49 lines (38 loc) · 1.13 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
const title = document.querySelector('title')
title.textContent = "Alarm clock app";
function time_convert(num) {
let minutes = Math.floor((num % 3600) / 60);
let seconds = num % 60;
return minutes.toString().padStart(2, '0') + ":" + seconds.toString().padStart(2, '0');
}
function setAlarm() {
const alarmSetInputEl = document.getElementById("alarmSet");
let timeEl = Number(alarmSetInputEl.value);
const timeRemainingCounterEl = document.getElementById("timeRemaining");
const interval = setInterval(() => {
timeEl--;
timeRemainingCounterEl.textContent = `Time Remaining: ${time_convert(timeEl)}`;
if (timeEl <= 0) {
clearInterval(interval);
timeRemainingCounterEl.textContent = "Done!";
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;