gpt4 book ai didi

javascript:无法让 setTimeout 正常工作

转载 作者:行者123 更新时间:2023-11-28 13:35:44 25 4
gpt4 key购买 nike

我在带有传单的 map 上有一系列对象。我希望我的 map 在一段时间后平移到每个对象。

var idx = 0;
function getNextPoint(point) {
console.log(idx);
map.panTo(new L.LatLng(point.y,point.x), true, 1, 0.5);
idx++;
if (idx < points.length) {
setTimeout(getNextCenter(points[idx]), 4000);
}
}

setTimeout(getNextPoint(points[idx]), 4000);

所以我希望这段代码在 4s 后执行 getNextPoint(),平移到第一个点,然后设置下一个超时,并在 4s 后再次执行该方法,依此类推,直到达到 idx max。

但这不是它正在做的事情。它会快速循环遍历所有点,并且只生成一个可见的平移。

我做错了什么?

最佳答案

尝试将超时函数包装在另一个函数中:

setTimeout(function() {
getNextPoint(points[idx]);
}, 4000);

关于javascript:无法让 setTimeout 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21506379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com