Skip to content

Instantly share code, notes, and snippets.

@imbcmdth
Last active August 29, 2015 13:56
Show Gist options
  • Save imbcmdth/8832780 to your computer and use it in GitHub Desktop.
Save imbcmdth/8832780 to your computer and use it in GitHub Desktop.
rAF shim
window.requestAnimFrame = (function(global) {
var callbackList = [],
timer,
nextTimeSlot = 0;
function processCallbacks () {
var localCBList = callbackList,
now = +(new Date), cb;
callbackList = [];
timer = null;
nextTimeSlot = now + 17; // ~59 fps
while (cb = localCBList.shift()) {
cb(now);
}
}
function universalRAFShim (callback) {
callbackList.unshift(callback);
if (!timer) {
timer = global.setTimeout(processCallbacks, Math.max(nextTimeSlot - (new Date), 0));
}
}
return global.requestAnimationFrame ||
global.webkitRequestAnimationFrame ||
global.mozRequestAnimationFrame ||
global.oRequestAnimationFrame ||
global.msRequestAnimationFrame ||
universalRAFShim;
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment