-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
47 lines (34 loc) · 1.39 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
let targetDate = new Date(2021, 11, 1, 9, 30).getTime();
console.log(targetDate);
let timer = setInterval(function () {
let now = Date.now();
let leftTime = targetDate - now;
let days = Math.floor(leftTime / (1000 * 60 * 60 * 24));
let hours = Math.floor((leftTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((leftTime % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((leftTime % (1000 * 60)) / 1000);
function check(val) {
if (String(val).length === 1) return "0" + val;
else if (leftTime < 0) {
clearInterval(timer);
return "00";
}
else return String(val);
}
days = check(days);
hours = check(hours);
minutes = check(minutes);
seconds = check(seconds);
function update(element, value) {
if (element.innerHTML !== value) {
element.previousElementSibling.classList.remove("action");
void element.offsetWidth;
element.previousElementSibling.classList.add("action");
setTimeout(() => element.innerHTML = value, 300);
}
}
update(document.getElementById("seconds"), seconds);
update(document.getElementById("minutes"), minutes);
update(document.getElementById("hours"), hours);
update(document.getElementById("days"), days);
}, 1000);