-
-
Save macouella/2d4e2d8d36bb08ee195e6408f469c5d4 to your computer and use it in GitHub Desktop.
Drop in replacements for setTimeout()/setInterval() that makes use of requestAnimationFrame() where possible for better performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.requestAnimFrame = (function() { | |
return window.requestAnimationFrame || | |
function(/* function */ callback, /* DOMElement */ element){ | |
window.setTimeout(callback, 1000 / 60); | |
}; | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Behaves the same as setInterval except uses requestAnimationFrame() where possible for better performance | |
* @param {function} fn The callback function | |
* @param {int} delay The delay in milliseconds | |
*/ | |
window.requestInterval = function(fn, delay) { | |
if( !window.requestAnimationFrame) 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; | |
} | |
/** | |
* Behaves the same as clearInterval except uses cancelRequestAnimationFrame() where possible for better performance | |
* @param {int|object} fn The callback function | |
*/ | |
window.clearRequestInterval = function(handle) { | |
window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) : clearInterval(handle); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Behaves the same as setTimeout except uses requestAnimationFrame() where possible for better performance | |
* @param {function} fn The callback function | |
* @param {int} delay The delay in milliseconds | |
*/ | |
window.requestTimeout = function(fn, delay) { | |
if( !window.requestAnimationFrame ) return window.setTimeout(fn, delay); | |
var start = new Date().getTime(), | |
handle = new Object(); | |
function loop(){ | |
var current = new Date().getTime(), | |
delta = current - start; | |
delta >= delay ? fn.call() : handle.value = requestAnimFrame(loop); | |
}; | |
handle.value = requestAnimFrame(loop); | |
return handle; | |
}; | |
/** | |
* Behaves the same as clearTimeout except uses cancelRequestAnimationFrame() where possible for better performance | |
* @param {int|object} fn The callback function | |
*/ | |
window.clearRequestTimeout = function(handle) { | |
window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) : clearTimeout(handle); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment