Skip to content

Instantly share code, notes, and snippets.

@compulim
Created January 31, 2020 23:29
Show Gist options
  • Save compulim/464dbe33238813e318c37f6a83f9b385 to your computer and use it in GitHub Desktop.
Save compulim/464dbe33238813e318c37f6a83f9b385 to your computer and use it in GitHub Desktop.
setIntervalAsync
function setIntervalAsync(fn, interval) {
let stopped;
let timeout;
const schedule = () => {
timeout = setTimeout(async () => {
await fn();
stopped || schedule();
}, interval);
};
schedule();
return () => {
clearTimeout(timeout);
stopped = true;
};
}
function clearIntervalAsync(interval) {
interval();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment