Skip to content

Instantly share code, notes, and snippets.

@sethladd
Created April 13, 2011 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sethladd/918352 to your computer and use it in GitHub Desktop.
Save sethladd/918352 to your computer and use it in GitHub Desktop.
function Timer() {
this.gameTime = 0;
this.lastTick = 0;
this.maxStep = 0.05;
this.lastTimestamp = 0;
}
Timer.prototype.step = function() {
var current = Date.now();
var delta = (current - this.lastTimestamp) / 1000;
this.gameTime += Math.min(delta, this.maxStep);
this.lastTimestamp = current;
}
Timer.prototype.tickDiff = function() {
var delta = this.gameTime - this.lastTick;
this.lastTick = this.gameTime;
return delta;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment