-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
81 lines (66 loc) · 2.34 KB
/
script.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
const currentTime = document.querySelector("h1"),
content = document.querySelector(".content"),
selectMenu = document.querySelectorAll("select"),
setAlarmBtn = document.querySelector("button");
let alarmTime, isAlarmSet = false,
ringtone = new Audio("./files/ringtone.mp3");
//Prints the Hours inside the select box
for(let i = 12; i>0; i--){
i = i < 10 ? "0" + i: i;
let option = `<option value="${i}">${i}</option>`;
selectMenu[0].firstElementChild.insertAdjacentHTML("afterend", option);
}
//Prints the minutes inside the textbox
for(let i = 59; i>0; i--){
i = i < 10 ? "0" + i: i;
let option = `<option value="${i}">${i}</option>`;
selectMenu[1].firstElementChild.insertAdjacentHTML("afterend", option);
}
//Prints the AM, PM inside the select box
for(let i = 2; i>0; i--){
let ampm = i == 1 ? "AM" : "PM";
let option = `<option value="${ampm}">${ampm}</option>`;
selectMenu[2].firstElementChild.insertAdjacentHTML("afterend", option);
}
setInterval(() => {
//getting hours, minutes, seconds
let date = new Date();
h = date.getHours();
m = date.getMinutes();
s = date.getSeconds();
ampm = "AM";
if(h >= 12){
h-= 12;
ampm = "PM";
}
//if hour value is 0. set the value to 12
h = h == 0 ? h = 12: h;
//adding 0 before hr, min, sec if this value is less than 10
h = h < 10 ? "0" + h : h;
m = m < 10 ? "0" + m : m;
s = s < 10 ? "0" + s : s;
//console.log(currentTime);
currentTime.innerText = (`${h}:${m}:${s} ${ampm}`);
if(alarmTime == `${h}:${m} ${ampm}`){
ringtone.play();
ringtone.loop = true;
}
}, 1000);
function setAlarm(){
if(isAlarmSet){
alarmTime = "";
ringtone.pause();
content.classList.remove("disable");
setAlarmBtn.innerText = "Set Alarm";
return isAlarmSet = false;
}
let time = `${selectMenu[0].value}:${selectMenu[1].value} ${selectMenu[2].value}`;
if(time.includes("Hour") || time.includes("Minutes") || time.includes("AM/PM")){
return alert("Please Enter a Valid Time.")
}
isAlarmSet = true;
alarmTime = time;
content.classList.add("disable");
setAlarmBtn.innerText = "Clear Alarm";
}
setAlarmBtn.addEventListener("click", setAlarm);