-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
47 lines (37 loc) · 1.53 KB
/
app.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
//Facilitar o uso do querySelector
const qs = e => document.querySelector(e);
//Recuperar campos do HTML
const secondsContainer = qs("#seconds");
const minutesContainer = qs("#minutes");
const hoursContainer = qs("#hours");
const daysContainer = qs("#days");
const nextYearContainer = qs("#year");
const spinnerLoading = qs("#loading");
const countdownContainer = qs("#countdown");
//Declarar o proximo ano
const nextYear = new Date().getFullYear() + 1;
const newYearTime = new Date(`January 01 ${nextYear} 00:00:00`);
nextYearContainer.textContent = nextYear;
const getTimeUnit = unit => unit < 10 ? "0" + unit : unit;
const insertCountdownValues = ({ days, hours, minutes, seconds }) => {
secondsContainer.textContent = getTimeUnit(seconds);
minutesContainer.textContent = getTimeUnit(minutes);
hoursContainer.textContent = getTimeUnit(hours);
daysContainer.textContent = getTimeUnit(days);
}
//Declarar a data atual
const updateCountdown = () => {
const currentTime = new Date();
const difference = newYearTime - currentTime;
const days = Math.floor(difference / 1000 / 60 / 60 / 24);
const hours = Math.floor(difference / 1000 / 60 / 60 ) % 24;
const minutes = Math.floor(difference / 1000 / 60 ) % 60;
const seconds = Math.floor(difference / 1000 ) % 60;
insertCountdownValues({ days, hours, minutes, seconds })
}
const handleCountdownDisplay = () => {
spinnerLoading.remove();
countdownContainer.style.display = "flex";
}
setTimeout(handleCountdownDisplay, 1000);
setInterval(updateCountdown, 1000);