更新时间:2022-12-30 13:50:05
这是因为闭包捕获的是变量 i
本身,而不是当前值.试试:
It's because the closure captures the variable i
itself, not the current value. Try:
for (var i = 0; i < a.length; i++) (function(i)
{
calcRoute(fixedLocation, my_cities[i].address, function(response) {
// i want here to have the current "i" here
});
}) (i);
这将为每次循环迭代创建一个新的 i
变量.
which will create a new i
variable for each loop iteration.