Skip to content

Instantly share code, notes, and snippets.

@cenullum
Last active August 28, 2022 00:34
Show Gist options
  • Save cenullum/8ae66127c60ac37628b8b82021114670 to your computer and use it in GitHub Desktop.
Save cenullum/8ae66127c60ac37628b8b82021114670 to your computer and use it in GitHub Desktop.
Javascript Infinite Loop with Delta Time
var lastUpdate = Date.now();
var lastFixed = Date.now();
var fixedTimer=0.02;
var updateInterval = setInterval(tick, 0);
var fixedInterval = setInterval(fixedTick,fixedTimer);
function tick() {
var now = Date.now();
var dt = now - lastUpdate;
lastUpdate = now;
update(dt);
render(dt);
}
function fixedTick(){
var now = Date.now();
var dt = now - lastFixed;
lastFixed = now;
fixedupdate(dt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment