Skip to content

Instantly share code, notes, and snippets.

@zhoufenfens
Created April 22, 2016 09:36
Show Gist options
  • Save zhoufenfens/85aba500200db3ea38125b8e5d71e59d to your computer and use it in GitHub Desktop.
Save zhoufenfens/85aba500200db3ea38125b8e5d71e59d to your computer and use it in GitHub Desktop.
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
window.requestInterval = function(fn, delay) {
if( !window.requestAnimationFrame &&
!window.webkitRequestAnimationFrame &&
!(window.mozRequestAnimationFrame && window.mozCancelRequestAnimationFrame) && !window.oRequestAnimationFrame &&
!window.msRequestAnimationFrame)
return window.setInterval(fn, delay);
var start = new Date().getTime(),
handle = new Object();
function loop() {
var current = new Date().getTime(),
delta = current - start;
if(delta >= delay) {
fn.call();
start = new Date().getTime();
}
handle.value = requestAnimFrame(loop);
};
handle.value = requestAnimFrame(loop);
return handle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment