Skip to content

Instantly share code, notes, and snippets.

@englishextra
Forked from joelambert/README
Last active October 13, 2017 15:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save englishextra/873c8f78bfda7cafc905f48a963df07b to your computer and use it in GitHub Desktop.
Save englishextra/873c8f78bfda7cafc905f48a963df07b 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
/*! http://paulirish.com/2011/requestanimationframe-for-smart-animating/
* http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
* requestAnimationFrame polyfill by Erik Moller. fixes from Paul Irish and Tino Zijdel
* MIT license
*/
(function(){var lastTime=0;var vendors=["ms","moz","webkit","o"];for(var x=0;x<vendors.length&&!window.requestAnimationFrame;++x){window.requestAnimationFrame=window[vendors[x]+"RequestAnimationFrame"];window.cancelAnimationFrame=window[vendors[x]+"CancelAnimationFrame"]||window[vendors[x]+"CancelRequestAnimationFrame"];}if(!window.requestAnimationFrame)window.requestAnimationFrame=function(callback,element){var currTime=new Date().getTime();var timeToCall=Math.max(0,16-(currTime-lastTime));var id=window.setTimeout(function(){callback(currTime+timeToCall);},timeToCall);lastTime=currTime+timeToCall;return id;};if(!window.cancelAnimationFrame)window.cancelAnimationFrame=function(id){clearTimeout(id);};}());
/*!
* requestAnimationFrame() shim by Paul Irish
* gist.github.com/joelambert/1002116
* gist.github.com/englishextra/873c8f78bfda7cafc905f48a963df07b
* paulirish.com/2011/requestanimationframe-for-smart-animating/
*/
var requestAnimFrame=(function(){return window.requestAnimationFrame||function(callback,element){window.setTimeout(callback,1000/60);};})();
/*!
* Behaves the same as setInterval except uses requestAnimationFrame() where possible for better performance
* modified gist.github.com/joelambert/1002116
* the fallback function requestAnimFrame is incorporated
* gist.github.com/joelambert/1002116
* gist.github.com/englishextra/873c8f78bfda7cafc905f48a963df07b
* jsfiddle.net/englishextra/sxrzktkz/
* @param {Object} fn The callback function
* @param {Int} delay The delay in milliseconds
* requestInterval(fn, delay);
*/
var requestInterval = function (fn, delay) {
var requestAnimFrame = (function () {
return window.requestAnimationFrame || function (callback, element) {
window.setTimeout(callback, 1000 / 60);
};
})(),
start = new Date().getTime(),
handle = {};
function loop() {
handle.value = requestAnimFrame(loop);
var current = new Date().getTime(),
delta = current - start;
if (delta >= delay) {
fn.call();
start = new Date().getTime();
}
}
handle.value = requestAnimFrame(loop);
return handle;
};
/*!
* Behaves the same as clearInterval except uses cancelRequestAnimationFrame()
* where possible for better performance
* gist.github.com/joelambert/1002116
* gist.github.com/englishextra/873c8f78bfda7cafc905f48a963df07b
* jsfiddle.net/englishextra/sxrzktkz/
* @param {Int|Object} handle function handle, or function
* clearRequestInterval(handle);
*/
var clearRequestInterval = function (handle) {
if (window.cancelAnimationFrame) {
window.cancelAnimationFrame(handle.value);
} else {
window.clearInterval(handle);
}
};
/*!
* Behaves the same as setTimeout except uses requestAnimationFrame()
* where possible for better performance
* modified gist.github.com/joelambert/1002116
* the fallback function requestAnimFrame is incorporated
* gist.github.com/joelambert/1002116
* gist.github.com/englishextra/873c8f78bfda7cafc905f48a963df07b
* jsfiddle.net/englishextra/dnyomc4j/
* @param {Object} fn The callback function
* @param {Int} delay The delay in milliseconds
* requestTimeout(fn,delay);
*/
var requestTimeout = function (fn, delay) {
var requestAnimFrame = (function () {
return window.requestAnimationFrame || function (callback, element) {
window.setTimeout(callback, 1000 / 60);
};
})(),
start = new Date().getTime(),
handle = {};
function loop() {
var current = new Date().getTime(),
delta = current - start;
if (delta >= delay) {
fn.call();
} else {
handle.value = requestAnimFrame(loop);
}
}
handle.value = requestAnimFrame(loop);
return handle;
};
/*!
* Behaves the same as clearTimeout except uses cancelRequestAnimationFrame()
* where possible for better performance
* gist.github.com/joelambert/1002116
* gist.github.com/englishextra/873c8f78bfda7cafc905f48a963df07b
* jsfiddle.net/englishextra/dnyomc4j/
* @param {Int|Object} handle The callback function
* clearRequestTimeout(handle);
*/
var clearRequestTimeout = function (handle) {
if (window.cancelAnimationFrame) {
window.cancelAnimationFrame(handle.value);
} else {
window.clearTimeout(handle);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment