Skip to content

Instantly share code, notes, and snippets.

@jimmont
Forked from paulirish/rAF.js
Last active December 17, 2015 03:29
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 jimmont/5543741 to your computer and use it in GitHub Desktop.
Save jimmont/5543741 to your computer and use it in GitHub Desktop.
// paulirish, emoller et al. MIT license
window.requestAnimationFrame = window.requestAnimationFrame || function(callback, element){
var fn, cfn, w = window, lastTime = 0, v, vendors = 'webkit,moz,ms,o'.split(',');
while(!fn && (v = vendors.shift())){
fn = w[v+'RequestAnimationFrame'];
cfn = w[v+'CancelAnimationFrame'] || w[v+'CancelRequestAnimationFrame'];
};
w.requestAnimationFrame = fn || function(callback, element){
var id, currTime = new Date().getTime(), timeToCall = Math.max(0, 16 - (currTime - lastTime));
id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall);
lastTime = currTime + timeToCall;
};
w.cancelAnimationFrame = cfn || w.clearTimeout;
fn(callback, element);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment