Skip to content

Instantly share code, notes, and snippets.

@josfaber
Last active November 20, 2021 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josfaber/c8434b656641a967c812bc563ac49d53 to your computer and use it in GitHub Desktop.
Save josfaber/c8434b656641a967c812bc563ac49d53 to your computer and use it in GitHub Desktop.
requestAnimationFrame rAF
/**
* requestAnimationFrame polyfill
* Based on: https://gist.github.com/paulirish/1579671
* Tweaked by: Bradley - https://gist.github.com/bradley
*/
(function() {
var lastTime = 0,
vendors = ['ms', 'moz', 'webkit', 'o'],
x,
length,
currTime,
timeToCall;
for (x = 0, length = vendors.length; x < 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) {
currTime = new Date().getTime();
timeToCall = Math.max(0, 16 - (currTime - lastTime));
lastTime = currTime + timeToCall;
return window.setTimeout(function() {
callback(currTime + timeToCall);
},
timeToCall);
};
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment