【发布时间】:2017-03-03 04:33:57
【问题描述】:
我在 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);
}
});
}
这个函数循环遍历一个字母数组,每 3 秒我可以对每个字母做一些事情。但问题是,当这个循环结束并完成时,我不能再重复这个过程......递归调用会产生堆栈溢出(Uncaught RangeError: Maximum call stack size exceeded)lol
告诉我怎么做!谢谢!!
BR
【问题讨论】:
标签: javascript arrays recursion