Created
January 31, 2020 23:29
-
-
Save compulim/464dbe33238813e318c37f6a83f9b385 to your computer and use it in GitHub Desktop.
setIntervalAsync
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
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