-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer-date.js
116 lines (82 loc) · 1.83 KB
/
timer-date.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var TimerJS = {
list: new Array(),
timeToString: function(time){
var minute = Math.floor(time / 60);
if (minute < 10) {
minute = '0' + minute;
}
var second = time % 60;
if (second < 10) {
second = '0' + second;
}
return minute + ':' + second;
},
counterInterval: function(element, text, time, timer, hooks)
{
element.innerHTML = TimerJS.timeToString(time) + text;
if (time <= 0) {
var finish = element.getAttribute('finish');
if (finish == null) {
element.innerHTML = '';
}else{
element.innerHTML = finish;
}
TimerJS.clearTimer(timer);
if (typeof hooks.onFinish != "undefined") {
hooks.onFinish(element);
}
}
},
findTimerId: function(timerId)
{
return TimerJS.list.indexOf(timerId);
},
clearTimer: function(timerId)
{
var timer = TimerJS.findTimerId(timerId);
if (timer == -1) {
return false;
}
clearInterval(timerId);
return TimerJS.list.splice(timer, 1);
},
start: function(element, hooks)
{
var timers = new Array();
document.querySelectorAll(element).forEach( function(element, index) {
var text = element.getAttribute('text');
var time = element.getAttribute('time');
if (time == null) {
return false;
}
time = Number(time);
if (time <= 0) {
return false;
}
if (text == null) {
text = '';
}else{
text = ' ' + text;
}
var timer = setInterval(function(){
time--;
TimerJS.counterInterval(element, text, time, timer, hooks);
}, 1000);
timers.push(timer);
TimerJS.list.push(timer);
TimerJS.counterInterval(element, text, time, timer, hooks);
});
return timers;
},
stop: function(timerId)
{
TimerJS.clearTimer(timerId);
},
stopAll: function()
{
let timers = TimerJS.list.slice(0);
for (var i = 0; i < timers.length; i++) {
this.stop(timers[i]);
}
}
};