-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtick-timer.js
43 lines (41 loc) · 1.12 KB
/
tick-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
// Returns the elasped time since last
// tick. Also overriding so the time
// can be maually set so as not a
// realtime timer
function TickTimer (options) {
extend(this, eventMixin);
this.lastTickTime = Date.now();
}
TickTimer.prototype = {
start: function () {
if (this.isStarted===true) {
return;
}
this.lastTickTime = Date.now();
this.isStarted = true;
this.trigger("started", this);
},
stopTicker: function () {
if (!this.isStarted) {
return;
}
this.isStarted = false;
this.trigger("stopped", this);
},
reset: function () {
this.trigger("reset", this);
this.lastTickTime = Date.now();
},
tick: function () {
if (this.usesEvents) {
this.trigger("tick", this);
}
this.lastTickTime = Date.now();
},
getTimeSinceLastTickMS: function () {
return (this.isStarted) ? Date.now() - this.lastTickTime : 0;
},
getTimeSinceLastTickSec: function () {
return (this.isStarted) ? (Date.now() - this.lastTickTime) / 1000 : 0;
}
};