Created
December 13, 2021 06:51
-
-
Save YuraKostin/de8f3f941d5f3efa3c6e5e8715b9e4bd to your computer and use it in GitHub Desktop.
timeoutChain
This file contains hidden or 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 timeoutChain = (fn, { ms } = { ms: 0 }) => { | |
const timer = { | |
id: null, | |
unsubscribe(done = () => {}) { | |
clearTimeout(this.id); | |
this.isRunning = false; | |
done(); | |
}, | |
isRunning: true, | |
}; | |
function run() { | |
timer.id = setTimeout(() => { | |
fn(() => timer.unsubscribe()); | |
if (timer.isRunning) { | |
run(); | |
} | |
}, ms); | |
} | |
run(); | |
return (onDone) => () => timer.unsubscribe(onDone); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment