-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
158 lines (123 loc) · 3.94 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
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
const title = document.querySelector(".title");
const countdown = document.querySelector(".countdown");
const eventNameInput = document.querySelector("#event-name");
const eventDateInput = document.querySelector("#event-date");
const enableSoundCheckbox = document.querySelector("#enable-sound");
let interval,
enableSound = false,
delay = 3000;
//* event listeners
const setupEventListeners = () => {
document.querySelector(".form").addEventListener("submit", handleSubmit);
document.querySelector("#reset").addEventListener("click", handleReset);
document.querySelector("#new").addEventListener("click", handleReset);
};
//* event handlers
const handleReset = () => {
// stop the timer
clearInterval(interval);
title.textContent = "Countdown Timer";
countdown.querySelector(".running").hidden = false;
countdown.querySelector(".expired").hidden = true;
countdown.hidden = true;
updateCountdown({ days: 0, hours: 0, minutes: 0, seconds: 0 });
// clear local storage
localStorage.removeItem("countdownData");
// reset form
setDefaultEventDate();
eventNameInput.value = "";
enableSoundCheckbox.checked = false;
};
const handleSubmit = (e) => {
e.preventDefault();
const eventDate = new Date(eventDateInput.value);
// check for past dates
if (getTimeLeft(eventDate) <= 0) {
alert("Please choose future date");
return;
}
// show countdown timer
countdown.hidden = false;
title.textContent = eventNameInput.value;
enableSound = enableSoundCheckbox.checked;
// save to local storage
localStorage.setItem(
"countdownData",
JSON.stringify({
eventName: eventNameInput.value,
eventDate,
enableSound,
})
);
// run timer every second
runCountdown(eventDate);
interval = setInterval(() => runCountdown(eventDate), 1000);
};
//* countdown
const initCountdown = () => {
const countdownData = JSON.parse(localStorage.getItem("countdownData"));
if (!Object.is(countdownData, null)) {
countdown.hidden = false;
title.textContent = countdownData.eventName;
enableSound = countdownData.enableSound;
const eventDate = new Date(countdownData.eventDate);
runCountdown(eventDate);
interval = setInterval(() => runCountdown(eventDate), 1000);
}
};
const runCountdown = (eventDate) => {
const timeLeft = getTimeLeft(eventDate);
if (timeLeft <= 0) {
// stop timer
clearInterval(interval);
title.textContent = "Countdown Over";
countdown.querySelector(".running").hidden = true;
countdown.querySelector(".expired").hidden = false;
handleSound(timeLeft);
} else {
const days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
const hours = Math.floor(
(timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((timeLeft % (1000 * 60)) / 1000);
updateCountdown({
days,
hours,
minutes,
seconds,
});
}
};
const updateCountdown = ({ days, hours, minutes, seconds }) => {
countdown.querySelector(".days").textContent = formatTime(days);
countdown.querySelector(".hours").textContent = formatTime(hours);
countdown.querySelector(".minutes").textContent = formatTime(minutes);
countdown.querySelector(".seconds").textContent = formatTime(seconds);
};
//* helpers
const handleSound = (timeLeft) => {
const isTimeout = timeLeft < -delay;
if (enableSound && !isTimeout) {
playSound();
}
};
const playSound = () => {
const sound = new Audio("./notification.mp3");
sound.play();
};
const formatTime = (time) => {
return time < 10 ? `0${time}` : time;
};
const getTimeLeft = (eventDate) => {
return eventDate - new Date().getTime();
};
const setDefaultEventDate = () => {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
eventDateInput.value = tomorrow.toISOString().split("T")[0];
};
//* initialize
setDefaultEventDate();
setupEventListeners();
initCountdown();