问题如下
编写一个函数,该函数将遍历整数列表,并在延迟3秒后打印每个元素的索引
常见的写法如下:
const arr = [10, 12, 15, 21];
for (var i = 0; i < arr.length; i++) {
setTimeout(function() {
console.log('The index of this number is: ' + i);
}, 3000);
}
如果运行上面代码,3 秒延迟后,实际上每次打印输出是 4,而不是期望的 0,1,2,3 。
为了正确理解为什么会发生这种情况,了解为什么会在 JavaScript 中发生这种情况将非常有用。
原因是因为 setTimeout 函数创建了一个可以访问其外部作用域的函数(闭包),该作用域是包含索引 i 的循环。 经过 3 秒后,执行该函数并打印出 i 的值,该值在循环结束时为 4,因为它循环经过0,1,2,3,4并且循环最终停止在 4。
所以应该写成如下的样子
const arr = [10, 12, 15, 21];
for (var i = 0; i < arr.length; i++) {
setTimeout(function(i_local){
return function () {
console.log('The index of this number is: ' + i_local);
}
}(i), 3000)
}