-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (103 loc) · 3.55 KB
/
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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const display = document.getElementById('clock');
// set audio for alarm
const audio = new Audio('https://assets.mixkit.co/sfx/preview/mixkit-alarm-digital-clock-beep-989.mp3');
audio.loop = true;
let alarmTime = null;
let alarmTimeout = null;
const myList = document.querySelector('#myList');
const addAlarm = document.querySelector('.setAlarm')
const alarmList = []; // Stores all the alarms being set
// let count =1;
// Plays the alarm audio at correct time
function ringing(now){
audio.play();
alert(`Hey! it is ${now}`)
}
// updates time every second
function updateTime() {
var today = new Date();
const hour = formatTime(today.getHours());
const minutes = formatTime(today.getMinutes());
const seconds = formatTime(today.getSeconds());
const now = `${hour}:${minutes}:${seconds}`;
display.innerText=`${hour}:${minutes}:${seconds}`;
// check if the alarmList includes the current time , "now"
// if yes, ringing() is called
if(alarmList.includes(now) ){
ringing(now);
}
}
// set the correct format of time
// converts "1:2:3" to "01:02:03"
function formatTime(time) {
if ( time < 10 && time.length != 2) {
return '0' + time;
}
return time;
}
// function to clear/stop the currently playing alarm
function clearAlarm() {
audio.pause();
if (alarmTimeout) {
clearTimeout(alarmTimeout);
alert('Alarm cleared');
}
}
// removes an alarm from the unordered list and the webpage when "Delete Alarm" is clicked
myList.addEventListener('click', e=> {
console.log("removing element")
if(e.target.classList.contains("deleteAlarm")){
e.target.parentElement.remove();
}
})
// removes an alarm from the array when "Delete Alarm" is clicked
remove = (value) => {
let newList = alarmList.filter((time) => time != value);
alarmList.length = 0; // Clear contents
alarmList.push.apply(alarmList, newList);
console.log("newList", newList);
console.log("alarmList", alarmList);
}
// Adds newAlarm to the unordered list as a new list item on webpage
function showNewAlarm(newAlarm){
const html =`
<li class = "time-list">
<span class="time">${newAlarm}</span>
<button class="deleteAlarm time-control" id="delete-button" onclick = "remove(this.value)" value=${newAlarm}>Delete Alarm</button>
</li>`
myList.innerHTML += html
};
// event to set a new alarm whenever the form is submitted
addAlarm.addEventListener('submit', e=> {
e.preventDefault();
// const newAlarm = addAlarm.alarmTime.value;
let new_h=formatTime(addAlarm.a_hour.value);
if(new_h === '0'){
new_h = '00'
}
let new_m=formatTime(addAlarm.a_min.value);
if(new_m === '0'){
new_m = '00'
}
let new_s=formatTime(addAlarm.a_sec.value);
if(new_s === '0'){
new_s = '00'
}
const newAlarm = `${new_h}:${new_m}:${new_s}`
// add newAlarm to alarmList
if(isNaN(newAlarm)){
if(!alarmList.includes(newAlarm)){
alarmList.push(newAlarm);
console.log(alarmList);
console.log(alarmList.length);
showNewAlarm(newAlarm);
addAlarm.reset();
} else{
alert(`Alarm for ${newAlarm} already set.`);
}
} else{
alert("Invalid Time Entered")
}
})
// calls updateTime() every second
setInterval(updateTime, 1000);