-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarm.js
141 lines (120 loc) · 5.18 KB
/
alarm.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
// Display Current Time
let currentTimeDiv = document.querySelector(".current-time-box");
setInterval(displayTime, 1000);
function displayTime() {
let d = new Date();
let hours = d.getHours() - 12; //getting time in 12 format
hours = hours < 0 ? d.getHours() : hours; //Changing time at night 12
let minutes = d.getMinutes();
let seconds = d.getSeconds();
let ampm = d.getHours() >= 12 ? "PM" : "AM";
minutes = minutes < 10 ? "0" + minutes : minutes; //Placing 0 before minutes
let currentTime = hours + ":" + minutes + ":" + seconds + " " + ampm;
currentTimeDiv.innerHTML = `<p style="text-align:center;color:#ecf0f1">Time is ${currentTime}</p>`;
}
//Set Alarm
function setAlarm() {
// Validating Input fields
let inputHour = document.querySelector("#hour").value;
let inputMinutes = document.querySelector("#minutes").value;
let alarmBox = document.querySelector(".alarm-box");
alarmBox.style.visibility = "visible";
if (inputHour == "" || inputMinutes == "") {
document.querySelector("#hour").value = "";
document.querySelector("#minutes").value = "";
var msg = "You can't leave input fields blank. Please fill the value.";
document.querySelector(".error-msg").innerHTML = `<h3>${msg}</h3>`;
} else if (isNaN(inputHour) || isNaN(inputHour)) {
document.querySelector("#hour").value = "";
document.querySelector("#minutes").value = "";
var msg =
"You can fill only numbers in input fields.Please fill numeric value.";
document.querySelector(".error-msg").innerHTML = `<h3>${msg}</h3>`;
} else {
//If Erros msg is present the Hide the Error msg
document.querySelector(".error-msg").style.display = "none";
// Disabling Inputs fields and buttons
document.querySelector("#hour").setAttribute("disabled", true);
document.querySelector("#minutes").setAttribute("disabled", true);
document.querySelector(".set-btn").setAttribute("disabled", true);
document.querySelector("#am-pm").setAttribute("disabled", true);
document.querySelector("#hour").style.backgroundColor = "#e74c3c";
document.querySelector("#minutes").style.backgroundColor = "#e74c3c";
document.querySelector(".set-btn").style.backgroundColor = "#e74c3c";
document.querySelector("#am-pm").style.backgroundColor = "#e74c3c";
// Get the alarm time
let uHours = document.querySelector("#hour").value;
let uMinutes = document.querySelector("#minutes").value;
let uAmPm = document.querySelector("#am-pm").value;
uMinutes = uMinutes < 10 ? "0" + uMinutes : uMinutes;
// Save data to localstorgae
localStorage.setItem("uHours", uHours);
localStorage.setItem("uMinutes", uMinutes);
localStorage.setItem("uamPm", uAmPm);
console.log(localStorage);
// Display alarm box
let alarmBox = document.querySelector(".alarm-box");
alarmBox.style.visibility = "visible";
let alarmTimeText = document.querySelector(".alarm-box h1");
alarmTimeText.innerText = uHours + " " + ": " + uMinutes + " " + uAmPm;
// Play sound on alarm time
let d = new Date();
let hours = d.getHours() - 12;
// hours = hours < 0 ? d.getHours() : hours; //Changing time at night 12
if (hours == 0) {
hours = 12;
} else if (hours < 0) {
hours = d.getHours();
}
let minutes = d.getMinutes();
let seconds = d.getSeconds();
let ampm = d.getHours() >= 12 ? "PM" : "AM";
minutes = minutes < 10 ? "0" + minutes : minutes; //Placing 0 before minutes
let uH = localStorage.getItem("uHours");
let uM = localStorage.getItem("uMinutes");
let uAP = localStorage.getItem("uamPm");
let timeToAlarm = (parseInt(uH) * 60 + parseInt(uM)) * 60 * 1000;
let timeWhenAlarmIsSet =
(parseInt(hours) * 60 + parseInt(minutes)) * 60 * 1000;
let timeToPlay =
timeToAlarm - timeWhenAlarmIsSet - parseInt(seconds) * 1000;
console.log(uH, uM);
console.log(hours);
console.log(minutes);
console.log(seconds);
console.log(timeToAlarm);
console.log(timeWhenAlarmIsSet);
console.log(timeToPlay);
if (timeToPlay >= 0 && uAP == ampm) {
setTimeout(() => {
let sound = new Audio();
sound.src = "ringtone1.mp3";
sound.loop = true;
sound.play();
// Stop Alarm
let stopBtn = document.createElement("button");
stopBtn.classList.add("delete-btn");
stopBtn.innerText = "Stop Alarm";
document.querySelector(".alarm-box").appendChild(stopBtn);
stopBtn.addEventListener("click", function () {
sound.pause();
});
}, Math.abs(timeToPlay));
} else if (timeToPlay < 0 && uAP == ampm) {
document.querySelector(".alarm-box").innerHTML =
"<h4 style='color:red'>Please Set proper alarm. You Can't set alarm for past time.</h4>";
let refreshBtn = document.createElement("button");
refreshBtn.classList.add("delete-btn");
refreshBtn.innerText = "Refresh";
document.querySelector(".alarm-box").appendChild(refreshBtn);
refreshBtn.addEventListener("click", function () {
window.location.href = "index.html";
});
}
}
}
//Delete btn reset local storage
function deleteAlarm() {
localStorage.clear();
window.location.href = "index.html";
}