-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (112 loc) · 3.58 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
136
137
138
139
140
141
142
143
144
145
const currentTime = document.querySelector("#current-time");
const setHours = document.querySelector("#hours");
const setMinutes = document.querySelector("#minutes");
const setSeconds = document.querySelector("#seconds");
const setAmPm = document.querySelector("#am-pm");
const setAlarmButton = document.querySelector("#submitButton");
const alarmContainer = document.querySelector("#alarms-container");
// Adding Hours, Minutes, Seconds in DropDown Menu
window.addEventListener("DOMContentLoaded", (event) => {
dropDownMenu(1, 12, setHours);
dropDownMenu(0, 59, setMinutes);
dropDownMenu(0, 59, setSeconds);
setInterval(getCurrentTime, 1000);
fetchAlarm();
});
// Event Listener added to Set Alarm Button
setAlarmButton.addEventListener("click", getInput);
function dropDownMenu(start, end, element) {
for (let i = start; i <= end; i++) {
const dropDown = document.createElement("option");
dropDown.value = i < 10 ? "0" + i : i;
dropDown.innerHTML = i < 10 ? "0" + i : i;
element.appendChild(dropDown);
}
}
function getCurrentTime() {
let time = new Date();
time = time.toLocaleTimeString("en-US", {
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: true,
});
currentTime.innerHTML = time;
return time;
}
function getInput(e) {
e.preventDefault();
const hourValue = setHours.value;
const minuteValue = setMinutes.value;
const secondValue = setSeconds.value;
const amPmValue = setAmPm.value;
const alarmTime = convertToTime(
hourValue,
minuteValue,
secondValue,
amPmValue
);
setAlarm(alarmTime);
}
// Converting time to 24 hour format
function convertToTime(hour, minute, second, amPm) {
return `${parseInt(hour)}:${minute}:${second} ${amPm}`;
}
function setAlarm(time, fetching = false) {
const alarm = setInterval(() => {
if (time === getCurrentTime()) {
alert("Alarm Ringing");
}
console.log("running");
}, 500);
addAlaramToDom(time, alarm);
if (!fetching) {
saveAlarm(time);
}
}
// Alarms set by user Dislayed in HTML
function addAlaramToDom(time, intervalId) {
const alarm = document.createElement("div");
alarm.classList.add("alarm", "mb", "d-flex");
alarm.innerHTML = `
<div class="time">${time}</div>
<button class="btn delete-alarm" data-id=${intervalId}>Delete</button>
`;
const deleteButton = alarm.querySelector(".delete-alarm");
deleteButton.addEventListener("click", (e) => deleteAlarm(e, time, intervalId));
alarmContainer.prepend(alarm);
}
// Is alarms saved in Local Storage?
function checkAlarams() {
let alarms = [];
const isPresent = localStorage.getItem("alarms");
if (isPresent) alarms = JSON.parse(isPresent);
return alarms;
}
// save alarm to local storage
function saveAlarm(time) {
const alarms = checkAlarams();
alarms.push(time);
localStorage.setItem("alarms", JSON.stringify(alarms));
}
// Fetching alarms from local storage
function fetchAlarm() {
const alarms = checkAlarams();
alarms.forEach((time) => {
setAlarm(time, true);
});
}
function deleteAlarm(event, time, intervalId) {
const self = event.target;
clearInterval(intervalId);
const alarm = self.parentElement;
console.log(time);
deleteAlarmFromLocal(time);
alarm.remove();
}
function deleteAlarmFromLocal(time) {
const alarms = checkAlarams();
const index = alarms.indexOf(time);
alarms.splice(index, 1);
localStorage.setItem("alarms", JSON.stringify(alarms));
}