Created
October 22, 2017 13:21
-
-
Save Gomah/11a9228c3165ecf972ddd8a78cc8b3ad to your computer and use it in GitHub Desktop.
requestAnimationFrame polyfill
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
(() => { | |
let lastTime = 0; | |
const vendors = ['ms', 'moz', 'webkit', 'o']; | |
for (let x = 0; x < vendors.length && !window.requestAnimationFrame; x += 1) { | |
window.requestAnimationFrame = window[`${vendors[x]}RequestAnimationFrame`]; | |
window.cancelAnimationFrame = | |
window[`${vendors[x]}CancelAnimationFrame`] || | |
window[`${vendors[x]}CancelRequestAnimationFrame`]; | |
} | |
if (!window.requestAnimationFrame) | |
window.requestAnimationFrame = cb => { | |
const currTime = new Date().getTime(); | |
const timeToCall = Math.max(0, 16 - (currTime - lastTime)); | |
const id = window.setTimeout(() => { | |
cb(currTime + timeToCall); | |
}, timeToCall); | |
lastTime = currTime + timeToCall; | |
return id; | |
}; | |
if (!window.cancelAnimationFrame) | |
window.cancelAnimationFrame = id => { | |
clearTimeout(id); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment