-
Notifications
You must be signed in to change notification settings - Fork 33
/
clock.js
44 lines (43 loc) · 1.2 KB
/
clock.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
(function () {
window.clock = {};
window.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame;
clock.Clock = function () {
this.running = false;
this.interval = null;
this.t0 = new Date();
}
clock.Clock.prototype = {
tick: function () {
var t1 = new Date(),
td = (t1-this.t0)/1000;
this.t0 = t1;
this.ontick(td);
},
start: function (element) {
this.running = true;
var self = this, f;
if(window.requestAnimationFrame){
window.requestAnimationFrame(f = function () {
self.tick();
if(self.running){
window.requestAnimationFrame(f, element);
}
}, element);
}
else {
this.interval = window.setInterval(function() {
self.tick();
}, 1);
}
this.t0 = new Date();
},
stop: function() {
if(this.interval){
window.clearInterval(this.interval);
this.interval = null;
}
this.running = false;
},
ontick: function() {}
};
})();