-
Notifications
You must be signed in to change notification settings - Fork 0
/
scr.js
157 lines (139 loc) · 4.27 KB
/
scr.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
//Initial References
let timerRef = document.querySelector(".timer-display");
const hourInput = document.getElementById("hourInput");
const minuteInput = document.getElementById("minuteInput");
const activeAlarms = document.querySelector(".activeAlarms");
const setAlarm = document.getElementById("set");
let alarmsArray = [];
let alarmSound = new Audio("./alarm.mp3");
let initialHour = 0,
initialMinute = 0,
alarmIndex = 0;
//Append zeroes for single digit
const appendZero = (value) => (value < 10 ? "0" + value : value);
//Search for value in object
const searchObject = (parameter, value) => {
let alarmObject,
objIndex,
exists = false;
alarmsArray.forEach((alarm, index) => {
if (alarm[parameter] == value) {
exists = true;
alarmObject = alarm;
objIndex = index;
return false;
}
});
return [exists, alarmObject, objIndex];
};
//Display Time
function displayTimer() {
let date = new Date();
let [hours, minutes, seconds] = [
appendZero(date.getHours()),
appendZero(date.getMinutes()),
appendZero(date.getSeconds()),
];
//Display time
timerRef.innerHTML = `${hours}:${minutes}:${seconds}`;
//Alarm
alarmsArray.forEach((alarm, index) => {
if (alarm.isActive) {
if (`${alarm.alarmHour}:${alarm.alarmMinute}` === `${hours}:${minutes}`) {
alarmSound.play();
alarmSound.loop = true;
}
}
});
}
const inputCheck = (inputValue) => {
inputValue = parseInt(inputValue);
if (inputValue < 10) {
inputValue = appendZero(inputValue);
}
return inputValue;
};
hourInput.addEventListener("input", () => {
hourInput.value = inputCheck(hourInput.value);
});
minuteInput.addEventListener("input", () => {
minuteInput.value = inputCheck(minuteInput.value);
});
//Create alarm div
const createAlarm = (alarmObj) => {
//Keys from object
const { id, alarmHour, alarmMinute } = alarmObj;
//Alarm div
let alarmDiv = document.createElement("div");
alarmDiv.classList.add("alarm");
alarmDiv.setAttribute("data-id", id);
alarmDiv.innerHTML = `<span>${alarmHour}: ${alarmMinute}</span>`;
//checkbox
let checkbox = document.createElement("input");
checkbox.setAttribute("type", "checkbox");
checkbox.addEventListener("click", (e) => {
if (e.target.checked) {
startAlarm(e);
} else {
stopAlarm(e);
}
});
alarmDiv.appendChild(checkbox);
//Delete button
let deleteButton = document.createElement("button");
deleteButton.innerHTML = `<i class="fa-solid fa-trash-can"></i>`;
deleteButton.classList.add("deleteButton");
deleteButton.addEventListener("click", (e) => deleteAlarm(e));
alarmDiv.appendChild(deleteButton);
activeAlarms.appendChild(alarmDiv);
};
//Set Alarm
setAlarm.addEventListener("click", () => {
alarmIndex += 1;
//alarmObject
let alarmObj = {};
alarmObj.id = `${alarmIndex}_${hourInput.value}_${minuteInput.value}`;
alarmObj.alarmHour = hourInput.value;
alarmObj.alarmMinute = minuteInput.value;
alarmObj.isActive = false;
console.log(alarmObj);
alarmsArray.push(alarmObj);
createAlarm(alarmObj);
hourInput.value = appendZero(initialHour);
minuteInput.value = appendZero(initialMinute);
});
//Start Alarm
const startAlarm = (e) => {
let searchId = e.target.parentElement.getAttribute("data-id");
let [exists, obj, index] = searchObject("id", searchId);
if (exists) {
alarmsArray[index].isActive = true;
}
};
//Stop alarm
const stopAlarm = (e) => {
let searchId = e.target.parentElement.getAttribute("data-id");
let [exists, obj, index] = searchObject("id", searchId);
if (exists) {
alarmsArray[index].isActive = false;
alarmSound.pause();
}
};
//delete alarm
const deleteAlarm = (e) => {
let searchId = e.target.parentElement.parentElement.getAttribute("data-id");
let [exists, obj, index] = searchObject("id", searchId);
if (exists) {
e.target.parentElement.parentElement.remove();
alarmsArray.splice(index, 1);
}
};
window.onload = () => {
setInterval(displayTimer);
initialHour = 0;
initialMinute = 0;
alarmIndex = 0;
alarmsArray = [];
hourInput.value = appendZero(initialHour);
minuteInput.value = appendZero(initialMinute);
};