-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.js
67 lines (53 loc) · 1.08 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Timer = class Timer{
constructor(delay, callback){
this._delay = delay;
this._fn = callback || function nullFn(a, data){};
}
start(opt_data) {
if (this.timerId) this.stop();
var that = this;
var cb = function(){
that.callback.call({}, that, that.data);
}
this._timerId = setTimeout(cb, this.delay, opt_data);
return this;
}
startPeriodic(opt_data) {
if (this.timerId) this.stop();
var that = this;
var cb = function(){
that.callback.call({}, that, that.data);
}
this._timerId = setInterval(cb, this.delay, opt_data);
return this;
}
stop() {
clearInterval(this.timerId);
this.timerId = undefined;
return this;
}
restart(opt_data) {
this.stop();
this.start();
return this;
}
get timerId(){
return this._timerId;
}
set timerId(val){
this._timerId = val;
}
get delay(){
return this._delay;
}
set delay(val){
if (this.timerId) this.stop();
this._delay = val;
}
get callback(){
return this._fn;
}
set callback(val){
this._fn = val;
}
};