-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown.js
More file actions
executable file
·58 lines (42 loc) · 1.09 KB
/
countdown.js
File metadata and controls
executable file
·58 lines (42 loc) · 1.09 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
var countdown=function(end, elements, callback){
var _second = 1000,
_minute = _second*60,
_hour = _minute*60,
_day = _hour * 24,
end = new Date(end),
timer,
calculate = function (){
var now=new Date(),
remaining= end.getTime() - now.getTime(),
data;
if(isNaN(end)){
console.log('Invalid date/time');
return;
}
if(remaining<=0){
clearInterval(timer);
if(typeof callback === 'function'){
callback();
}
}else {
if(!timer){
timer=setInterval(calculate, _second);
}
data={
'days': Math.floor(remaining / _day),
'hours': Math.floor((remaining % _day) / _hour),
'minutes': Math.floor((remaining % _hour) / _minute),
'seconds': Math.floor((remaining % _minute) / _second)
}
if(elements.length){
for(x in elements){
var x = elements[x];
data[x]= ('00' + data[x]).slice(-2);
document.getElementById(x).innerHTML = data[x];
}
}
//console.log(data);
}
};
calculate();
}