-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (44 loc) · 1.53 KB
/
script.js
File metadata and controls
55 lines (44 loc) · 1.53 KB
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
function timer(id, deadline){
function getTimeRemaining(endtime) {
const t = Date.parse(endtime) - Date.parse(new Date()),
days = Math.floor( (t/(1000*60*60*24)) ),
seconds = Math.floor( (t/1000) % 60 ),
minutes = Math.floor( (t/1000/60) % 60 ),
hours = Math.floor( (t/(1000*60*60) % 24) );
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}
function getZero(num){
if (num >= 0 && num < 10) {
return '0' + num;
} else {
return num;
}
}
function setClock(selector, endtime) {
const timer = document.querySelector(selector),
days = timer.querySelector("#days"),
hours = timer.querySelector('#hours'),
minutes = timer.querySelector('#minutes'),
seconds = timer.querySelector('#seconds'),
timeInterval = setInterval(updateClock, 1000);
updateClock();
function updateClock() {
const t = getTimeRemaining(endtime);
days.innerHTML = getZero(t.days);
hours.innerHTML = getZero(t.hours);
minutes.innerHTML = getZero(t.minutes);
seconds.innerHTML = getZero(t.seconds);
if (t.total <= 0) {
clearInterval(timeInterval);
}
}
}
setClock(id, deadline);
}
timer('.timer','2022-02-20');