-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
194 lines (146 loc) · 5.02 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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");
const ringTone = new Audio('./file/ringtone.mp3');
// Get the current date
const currentDate = new Date();
// Array of week days and months
const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
// Get the week day, month, day, and year
const weekDay = weekDays[currentDate.getDay()];
const month = months[currentDate.getMonth()];
const day = currentDate.getDate();
const year = currentDate.getFullYear();
// Update the HTML elements with the values
document.getElementById('week').textContent = weekDay;
document.getElementById('month').textContent = month;
document.getElementById('date').textContent = day;
document.getElementById('year').textContent = year;
// 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 updateClock() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timeString = hours.toString().padStart(2, '0') + ':'
+ minutes.toString().padStart(2, '0') + ':'
+ seconds.toString().padStart(2, '0');
}
// Update the clock every second
setInterval(updateClock, 1000);
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");
ringTone.play();
}
document.querySelector("#stopAlarm").addEventListener("click", stopAlarm);
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));
}
// ... your existing code ...
// used to stopAlarm
function stopAlarm() {
ringTone.pause();
}