-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown.js
More file actions
58 lines (46 loc) · 1.75 KB
/
countdown.js
File metadata and controls
58 lines (46 loc) · 1.75 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
56
57
58
const Countdown = (() => {
let nextMidnight = new Date();
nextMidnight.setHours(24,0,0,0);
const getRemainingTime = () => {
let now = new Date();
let time = (nextMidnight.getTime() - now.getTime())/1000;
if(time < 0) {
nextMidnight = new Date();
nextMidnight.setHours(24,0,0,0);
return getRemainingTime();
}
return time;
}
const parseTime = (time) => {
const hours = Math.floor(time/3600);
let rest = time-(hours*3600);
const minutes = Math.floor(rest/60);
rest = rest-(minutes*60);
const seconds = Math.floor(rest);
const milliseconds = (rest-seconds)*1000;
return [hours, minutes, seconds, milliseconds];
};
const formatTime = (parsedTime) => {
return '<span class="hours">' + parsedTime[0] + '</span><span class="hSep">:</span><span class="minutes">' + ("0" + parsedTime[1]).slice(-2) + '</span><span class="mSep">:</span><span class="seconds">' + ("0" + parsedTime[2]).slice(-2) + '</span>';
};
const els = [];
let timeout;
return (el) => {
els.push(el);
if(!timeout) {
const refresh = () => {
const parsedTime = parseTime(getRemainingTime());
const formattedTimes = formatTime(parsedTime);
for(let i = 0, iend = els.length; i < iend; i++) {
els[i].innerHTML = formattedTimes;
}
setTimeout(() => {
refresh();
}, parsedTime[3]);
};
refresh();
}else
el.innerHTML = formatTime(parseTime(getRemainingTime()));
};
})();
Countdown(document.getElementById("countdownInfo"));