Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Nicknyr/50a3b7fa539884971fe17472c9d9ae97 to your computer and use it in GitHub Desktop.
Save Nicknyr/50a3b7fa539884971fe17472c9d9ae97 to your computer and use it in GitHub Desktop.
Javascript: Looping over an array and calling a function in a sychronous manner using setInterval
// loop over the array using the iteratee function at the specified interval
function intervalForEach(array, iteratee, delay) {
let current = 0
let interval = setInterval(() => {
if (current === array.length) {
clearInterval(interval)
} else {
iteratee(array[current])
current++
}
}, delay)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment