Skip to content

Instantly share code, notes, and snippets.

@cecigarcia
Created February 18, 2020 08:35
Show Gist options
  • Save cecigarcia/70cf53bacf9ba04a029d4c1d1bd2c1cb to your computer and use it in GitHub Desktop.
Save cecigarcia/70cf53bacf9ba04a029d4c1d1bd2c1cb to your computer and use it in GitHub Desktop.
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