public
Last active

Delta Timing for JavaScript

  • Download Gist
DeltaTimer.js
JavaScript
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/**
@author <a href="mailto:aaditmshah@myopera.com">Aadit M Shah</a>
@overview Delta Timing for JavaScript.
@copyright 2012
@version 1.0.0
*/
 
/**
@description Creates a new Delta Timer with start and stop methods.
@constructor
@param {function} render The callback to render for animations.
@param {number} interval The interval of the timer in milliseconds.
*/
 
function DeltaTimer(render, interval) {
var timeout;
var lastTime;
 
this.start = start;
this.stop = stop;
 
/**
@description Start the timer.
@public
@function
@returns {number} The UTC time in milliseconds when the timer started.
*/
 
function start() {
timeout = setTimeout(loop, 0);
lastTime = Date.now();
return lastTime;
}
 
/**
@description Stop the timer.
@public
@function
@returns {number} The UTC time in milliseconds when the timer stopped.
*/
 
function stop() {
clearTimeout(timeout);
return lastTime;
}
 
/**
@description Loop the timer continuously and call the render function.
@private
@function
*/
 
function loop() {
var thisTime = Date.now();
var deltaTime = thisTime - lastTime;
var delay = Math.max(interval - deltaTime, 0);
timeout = setTimeout(loop, delay);
lastTime = thisTime + delay;
render(thisTime);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.