Last active
August 29, 2015 13:56
-
-
Save imbcmdth/8832780 to your computer and use it in GitHub Desktop.
rAF shim
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(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