Skip to content

Instantly share code, notes, and snippets.

@cecigarcia
Last active March 4, 2020 10:36
Show Gist options
  • Save cecigarcia/5a74503f8bc023a0cfaf057da9b52f27 to your computer and use it in GitHub Desktop.
Save cecigarcia/5a74503f8bc023a0cfaf057da9b52f27 to your computer and use it in GitHub Desktop.
const noop = () => {};
const requestTimeout = (fn, delay, registerCancel) => {
const start = new Date().getTime();
const loop = () => {
const delta = new Date().getTime() - start;
if (delta >= delay) {
fn();
registerCancel(noop);
return;
}
const raf = requestAnimationFrame(loop);
registerCancel(() => cancelAnimationFrame(raf));
};
const raf = requestAnimationFrame(loop);
registerCancel(() => cancelAnimationFrame(raf));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment