Created
February 18, 2020 08:35
-
-
Save cecigarcia/70cf53bacf9ba04a029d4c1d1bd2c1cb to your computer and use it in GitHub Desktop.
This file contains 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
const browserIsAnimationFrameIncompatible = | |
!window.requestAnimationFrame && | |
!window.webkitRequestAnimationFrame && | |
!window.mozRequestAnimationFrame && | |
!window.oRequestAnimationFrame && | |
!window.msRequestAnimationFrame; | |
const requestInterval = (fn, delay, registerCancel) => { | |
if (browserIsAnimationFrameIncompatible) { | |
const intervalId = window.setInterval(() => { | |
fn(); | |
clearInterval(intervalId); | |
}, delay); | |
return registerCancel(() => clearInterval(intervalId)); | |
} | |
const start = new Date().getTime(); | |
const loop = () => { | |
const delta = new Date().getTime() - start; | |
if (delta >= delay) { | |
fn(); | |
return registerCancel(noop); | |
} | |
const raf = requestAnimationFrame(loop); | |
return registerCancel(() => cancelAnimationFrame(raf)); | |
}; | |
const raf = requestAnimationFrame(loop); | |
return registerCancel(() => cancelAnimationFrame(raf)); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment