Skip to content

Instantly share code, notes, and snippets.

@Artem-Schander
Created November 22, 2019 17:44
Show Gist options
  • Save Artem-Schander/4b1f71b1eb9b5d54e26cb24d4cb75bc4 to your computer and use it in GitHub Desktop.
Save Artem-Schander/4b1f71b1eb9b5d54e26cb24d4cb75bc4 to your computer and use it in GitHub Desktop.
Recursive JavaScript function with delay in each loop
const run = (cb, repeats, timeout = 100, i = 0) => {
cb(i)
setTimeout(() => {
++ i
if (repeats >= i + 1) run(cb, repeats, timeout, i)
}, timeout)
}
run(i => {
console.log(i)
}, 42, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment