Skip to content

Instantly share code, notes, and snippets.

@pmcelhaney
Created July 2, 2012 16:30
Show Gist options
  • Save pmcelhaney/3034111 to your computer and use it in GitHub Desktop.
Save pmcelhaney/3034111 to your computer and use it in GitHub Desktop.
requestAnimationInterval
/*
requestAnimationInterval()
A sort of replacement for setInterval that uses requestAnimationFrame.
var myFunction = function (timestamp) {
// do some stuff
}
var interval = requestAnimationInterval(myFunction, 1000);
...
interval.stop();
...
interval.start();
*/
var requestAnimationInterval = function (callback, interval) {
var running = true;
var tick = function () {
requestAnimationFrame(function (timestamp) {
callback(timestamp);
if (running) setTimeout(tick, interval);
});
};
tick();
return {
stop: function () {
running = false;
},
start: function () {
if (!running) {
running = true;
tick();
}
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment