在循环中调用 setTimeout 函数
我是 javascript 新手,正在尝试在 for 循环中使用 setTimeout 调用函数。该循环针对节点列表的每个成员执行。
我发现我使用 setTimeout 调用的函数仅在循环的最后一次迭代期间实际执行。在下面的示例中,我想对 setTimeout 进行三个单独的调用,但我发现前两个调用被忽略。
function moveants(e, stepdistance) {
. . . . .
for(var i = 0; i < 3; i++)
{
var nextAnt = antgroup.childNodes[i]
nextAnt.count = 0;
nextAnt.member = i;
setTimeout(function () { takeStep(nextAnt, mouseclickX, mouseclickY, 10) }, 0);
}
}
function takeStep(ant, destX, destY, stepDistance) {
. . . .
. . . .
if( condition )
{
return;
}
else
{
takeStep(ant, destX, destY, stepDistance);
}
}
我看过 其他帖子 描述了多次调用 setTimeout 。令人惊讶的是(对我来说),如果我像这样将它们从 for 循环中取出,多个调用就会起作用。
setTimeout(function () { takeStep(antgroup.childNodes[0],
mouseclickX, mouseclickY, 10) }, 10);
setTimeout(function () { takeStep(antgroup.childNodes[1],
mouseclickX, mouseclickY, 10) }, 10);
setTimeout(function () { takeStep(antgroup.childNodes[2],
mouseclickX, mouseclickY, 10) }, 10);
我只是不明白为什么在 for 循环内调用它们和在循环外调用它们之间存在差异。
在每种情况下,我都从 setInterval 调用中获取有效的返回值。只是只有 for 循环的最后一次迭代才会真正执行该函数。
预先感谢您的任何帮助。
I'm new to javascript and am trying to call a function using setTimeout from within a for loop. The loop executes for each member of a nodeList.
I'm finding that the function I'm calling with setTimeout is only actually executing during the last iteration of the loop. In the example below, I would like to make three separate calls to setTimeout but I'm finding that the first two calls are ignored.
function moveants(e, stepdistance) {
. . . . .
for(var i = 0; i < 3; i++)
{
var nextAnt = antgroup.childNodes[i]
nextAnt.count = 0;
nextAnt.member = i;
setTimeout(function () { takeStep(nextAnt, mouseclickX, mouseclickY, 10) }, 0);
}
}
function takeStep(ant, destX, destY, stepDistance) {
. . . .
. . . .
if( condition )
{
return;
}
else
{
takeStep(ant, destX, destY, stepDistance);
}
}
I have seen other posts that describe making multiple calls to setTimeout. Amazingly (to me), the multiple calls will work if I simply take them out of the for loop like this.
setTimeout(function () { takeStep(antgroup.childNodes[0],
mouseclickX, mouseclickY, 10) }, 10);
setTimeout(function () { takeStep(antgroup.childNodes[1],
mouseclickX, mouseclickY, 10) }, 10);
setTimeout(function () { takeStep(antgroup.childNodes[2],
mouseclickX, mouseclickY, 10) }, 10);
I just can't figure out why there is a difference between calling them from within a for loop and calling them outside of one.
I am getting valid return values from the setInterval call in every case.. it's just that with only the last iteration of the for loop does the function actually execute.
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
nextAnt 将在每个循环中被覆盖,因此 takeStep() 将被调用 3 次,但始终使用相同的参数。
你可以尝试这个:
nextAnt will be overwritten on every loop, so takeStep() will be called 3 times, but always with the same arguments.
You may try this instead:
如果你不设置延迟,那为什么还要费心setTimeout呢?
在循环中,您的延迟设置为 0,在循环之外您使用了 10。此外,在循环外部,您已为 count 和 member 分配了值,但不在循环外部?
试试这个:
或者这个:
If your not setting a delay, then why bother with setTimeout?
In the loop your delay is set to 0, outside the loop you've used 10. Also, outside the loop you've assigned values to count and member, but not outside the loop?
Try this:
or this: