Skip to content

Instantly share code, notes, and snippets.

@toddpi314
Created June 7, 2012 22:14
Show Gist options
  • Save toddpi314/2891962 to your computer and use it in GitHub Desktop.
Save toddpi314/2891962 to your computer and use it in GitHub Desktop.
GameClock_ThreeOneFour_Impl1
DeepElement.ThreeOneFour.Common.Clock = new Class({
Implements: Events,
running: false,
interval: null,
t0: +new Date(),
rAFSupport: false,
initialize: function() {
self.requestAnimFrame = (function() {
return self.requestAnimationFrame || self.webkitRequestAnimationFrame || self.mozRequestAnimationFrame || self.oRequestAnimationFrame || self.msRequestAnimationFrame;
})();
if (self.requestAnimFrame) {
this.rAFSupport = true;
}
this.requestAnimationFrameHandler = this.requestAnimationFrameHandler.bind(this);
},
tick: function(t) {
var t1 = t != null ? t : +new Date();
var td = (t1 - this.t0) / 1000;
this.ontick(td);
this.t0 = t1;
},
requestAnimationFrameHandler: function(td) {
if (this.running) {
self.requestAnimFrame(this.requestAnimationFrameHandler);
this.tick(td);
}
},
start: function(element) {
this.running = true;
var selfFunc = this;
if (this.rAFSupport) {
self.requestAnimFrame(this.requestAnimationFrameHandler);
} else {
this.interval = self.setInterval(function() {
selfFunc.tick();
}, 1);
}
this.t0 = +new Date();
},
stop: function() {
if (this.interval) {
self.clearInterval(this.interval);
this.interval = null;
}
this.running = false;
},
ontick: function(td) {
this.fireEvent('tick', td);
}
});​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment