-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
48 lines (39 loc) · 1.18 KB
/
timer.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
window.addEventListener('DOMContentLoaded', function () {
'use strict';
let deadLine = '2020-11-20';
function getTimeRemaining(endtime) {
let t = Date.parse(endtime) - Date.parse(new Date());
let seconds = Math.floor((t / 1000) % 60),
minutes = Math.floor((t / 1000 / 60) % 60),
hours = Math.floor((t / (1000 * 60 * 60)));
return {
'total': t,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}
function setClock(id, endtime) {
let timer = document.getElementById(id),
hours = timer.querySelector('.hours'),
minutes = timer.querySelector('.minutes'),
seconds = timer.querySelector('.seconds'),
timeInterval = setInterval(updateClock, 1000);
function updateClock() {
let t = getTimeRemaining(endtime)
hours.textContent = t.hours;
minutes.textContent = t.minutes;
seconds.textContent = t.seconds;
if (t.total < 0) {
clearInterval(timeInterval);
}
if (t.seconds < 10) {
seconds.textContent = '0' + t.seconds;
}
if (t.minutes < 10) {
minutes.textContent = '0' + t.minutes;
}
}
}
setClock('timer', deadLine)
});