-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.js
59 lines (57 loc) · 1.6 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
// IE fix
Date.now = Date.now || function() { return +new Date(); };
function Timer (options) {
extend(this, eventMixin);
this.options = extend({
updateTime : 1000,
loops : true,
eventName : "fired",
stoppedEventName : "stop",
restartEventName : "restart"
}, options);
this.setIntervalID = undefined;
this._count = 0;
}
Timer.prototype = {
restart : function () {
clearInterval(this._intervalId);
this.trigger("restarting", this);
this.start();
return this;
},
start : function () {
if (this._intervalId) { return false; }
this.lastUpdate = new Date().getTime();
this._intervalId = setInterval(W.bind(this.update, this), 10 );
return true;
},
// Blackberry uses stop
stopTimer : function () {
if (this._intervalId) {
clearInterval(this._intervalId);
this._intervalId = undefined;
this.trigger(this.options.stoppedEventName, this);
return true;
}
return false;
},
update : function () {
var currentTime = new Date().getTime();
if (this.options.updateTime + this.lastUpdate < currentTime) {
this._count++;
this.trigger(this.options.eventName, this);
if (this.options.loops) {
this.lastUpdate = currentTime;
} else {
this.stopTimer();
}
}
return this;
},
count : function () {
return this._count;
},
resetCounter : function () {
this._count = 0;
}
};