-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathindex.js
35 lines (30 loc) · 848 Bytes
/
index.js
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
const alarmSubmit = document.getElementById("alarmSubmit");
const alarmStop = document.getElementById("alarmStop");
const audio = document.getElementById("alarmAudio");
// Add an event listener to the submit button
alarmSubmit.addEventListener("click", setAlarm);
// Add an event listener to the stop button
alarmStop.addEventListener("click", stopAlarm);
// function to play the alarm ring tone
function ringBell() {
audio.play();
}
// function to set alarm
function setAlarm(e) {
e.preventDefault();
const alarm = document.getElementById("alarm");
alarmDate = new Date(alarm.value);
now = new Date();
console.log(now);
let timeToAlarm = alarmDate - now;
console.log(timeToAlarm);
if (timeToAlarm >= 0) {
setTimeout(() => {
ringBell();
}, timeToAlarm);
}
}
// function to stop alarm
function stopAlarm(e) {
audio.pause();
}