Skip to content

Instantly share code, notes, and snippets.

@robashton
Created March 17, 2011 12:17
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 robashton/874223 to your computer and use it in GitHub Desktop.
Save robashton/874223 to your computer and use it in GitHub Desktop.
var timeAtLastFrame = new Date().getTime();
var idealTimePerFrame = 1000 / 30;
var leftover = 0.0;
var frames = 0;
function runApplication(){
setInterval(tick, 1000/30);
}
function tick() {
var timeAtThisFrame = new Date().getTime();
var timeSinceLastDoLogic = (timeAtThisFrame - timeAtLastFrame) + leftover;
var catchUpFrameCount = Math.floor(timeSinceLastDoLogic / idealTimePerFrame);
for(i = 0 ; i < catchUpFrameCount; i++){
controller.doLogic();
frames++;
}
renderer.renderScene();
leftover = timeSinceLastDoLogic - (catchUpFrameCount * idealTimePerFrame);
timeAtLastFrame = timeAtThisFrame;
}
@hendrysadrak
Copy link

Could update

var timeAtLastFrame = new Date().getTime();
var timeAtThisFrame = new Date().getTime();

to

var timeAtLastFrame = Date.now();
var timeAtThisFrame = Date.now();

It's much faster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment