I have this code in ES6:
function loopLettersEvery3Seconds() {
const letters = ['a', 'b', 'c'];
let offset = 0;
letters.forEach((letter, index) => {
setTimeout(() => {
// I do something with the letter, nothing important
}, 3000 + offset);
offset += 3000;
if (index === letters.length - 1) {
loopLettersEvery3Seconds(letters);
}
});
}
This function loop over an array of letters and every 3 second I can do something with every letter. But the thing is that when this loop comes to an end and finish, I can't repeat again the process... The recursive call generates an stack overflow (Uncaught RangeError: Maximum call stack size exceeded) lol
Tell me how to do it! Thanks!!
BR