-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
190 lines (168 loc) · 5.52 KB
/
Copy pathindex.js
File metadata and controls
190 lines (168 loc) · 5.52 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
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
// Define your DOM element references outside the event handler
const currentTimeDisplay = document.getElementById("current-time");
// to display current time in the UI
function updateCurrentTime() {
const currentTimeDisplay = document.getElementById("current-time");
if (currentTimeDisplay) {
const now = new Date();
const hours = now.getHours().toString().padStart(2, "0");
const minutes = now.getMinutes().toString().padStart(2, "0");
const seconds = now.getSeconds().toString().padStart(2, "0");
const ampm = now.getHours() < 12 ? "AM" : "PM";
currentTimeDisplay.innerText = `${hours}:${minutes}:${seconds} ${ampm}`;
} else {
console.error("Element with id 'current-time' not found.");
}
}
window.addEventListener("DOMContentLoaded", function () {
setInterval(updateCurrentTime, 1000);
});
let alarmList = [];
//for setting time in the alarm
const setAlarm = () => {
//variables for storing the time after clicking the setAlarm function
let hours = Number(document.querySelector("#setAlarm-hour").value);
let mins = Number(document.querySelector("#setAlarm-min").value);
let secs = Number(document.querySelector("#setAlarm-sec").value);
let ampm = document.querySelector("#setAlarm-ampm").value;
// if input box is empty.
if (hours === "" || mins === "" || secs === "" || ampm === "") {
alert("plz enter the Time!!");
return;
}
// if user put invalid time then show alert
if (ampm === "AM" && hours > 12) {
alert(`it shoud be PM.
plz check and enter again!!`);
return;
} else if (ampm === "PM" && hours <= 12) {
alert(`it shoud be AM.
plz check and enter again!!`);
return;
}
// if the input time already exist in the list then show alert
let ifExist = alarmList.find(
(ifexist) =>
hours === ifexist.hours && mins === ifexist.mins && secs === ifexist.secs
);
console.log("ifexist", ifExist);
if (ifExist !== undefined) {
alert("Alarm already exist!!");
document.querySelector("#setAlarm-hour").value = "";
document.querySelector("#setAlarm-min").value = "";
document.querySelector("#setAlarm-sec").value = "";
return;
}
// negative values for time is taken cared and value for hour should be less than 12.
if (
hours > 23 ||
hours < 0 ||
mins > 59 ||
mins < 0 ||
secs > 59 ||
secs < 0 ||
ampm === "AM/PM"
) {
alert("plz enter a valid Time");
// console.log(typeof hours, mins, secs, ampm);
return;
}
//created the new alarm object
const Alarm = {
id: Date.now(),
hours,
mins,
secs,
ampm,
setime: null,
};
//addalarm to the alarmList
alarmList.push(Alarm);
// console.log(alarmList);
//display the alarm in the UI
HandleDisplay(alarmList);
// calculate the time until the alarm
const currtime = new Date();
// console.log("**", currtime);
const alarmtime = new Date(
currtime.getFullYear(),
currtime.getMonth(),
currtime.getDate(),
hours,
mins,
secs
);
// console.log(">>>", alarmtime);
// getting the value of hours,mins and sec in currhours,currmins and currsecs.
const currhours = currtime.getHours();
const currmins = currtime.getMinutes();
const currsecs = currtime.getSeconds();
// calculate the time in miliseconds.
const currentmilliseconds =
(currhours * 60 * 60 + currmins * 60 + currsecs) *
1000;
// getting the value of hours,mins and sec in alarmhours,alarmmins and alarmsecs.
const alarmtimehours = alarmtime.getHours();
const alarmtimemins = alarmtime.getMinutes();
const alarmtimesecs = alarmtime.getSeconds();
// calculate the time in miliseconds.
const alarmtimemilliseconds =
(alarmtimehours * 60 * 60 +
alarmtimemins * 60 +
alarmtimesecs ) *
1000;
// calculate the time difference in miliseconds.
let alarmring = alarmtimemilliseconds - currentmilliseconds;
// console.log(alarmring);
if(alarmring<=0){
alarmring+=24*60*60*1000;
}
// setTimeout function for the alarm.
Alarm.setime = setTimeout(() => {
alert(`It's ${hours}:${mins}:${secs}${ampm}.
press OK to Turn OFF the alarm!!
`);
}, alarmring);
// clear the input field
document.querySelector("#setAlarm-hour").value = "";
document.querySelector("#setAlarm-min").value = "";
document.querySelector("#setAlarm-sec").value = "";
};
// delete alarm function
function handleDelete(i) {
const deletealarm = alarmList.find((alarm) => alarm.id === i);
clearTimeout(deletealarm.setime);
// Alarm.setime = null;
const updateAlarm = alarmList.map((items)=>{
if(items.id === i){
return {...items,setime:null}
}
return items;
})
HandleDisplay(updateAlarm);
let restalarms = alarmList.filter((alarm) => alarm.id !== i);
alarmList = restalarms;
// console.log("delete", deletealarm);
HandleDisplay(alarmList);
}
//display the alarm in the UI.
const HandleDisplay = (alarmList) => {
const setAlarmlist = document.querySelector("#setAlarm-list");
setAlarmlist.innerHTML = "";
setAlarmlist.innerHTML += alarmList.map((item) => {
const { hours, mins, secs, id, ampm } = item;
const hours1 = hours.toString().padStart(2, "0");
const mins1 = mins.toString().padStart(2, "0");
const secs1 = secs.toString().padStart(2, "0");
return `
<div class="parent">
<div class="current-time-alarm-left" key=item.id>
<div class="current-time-alarm">${hours1}:${mins1}:${secs1} ${ampm}</div>
</div>
<div class="current-time-alarm-right">
<button onclick="handleDelete(${id})">DELETE</button>
</div>
</div>
`;
});
};