Javascript setTimeout 与 for 循环中的函数

发布于 2024-12-20 16:41:36 字数 763 浏览 2 评论 0原文

使用 Javascript,我尝试循环遍历数组并在每个循环上执行一个带有时间延迟的函数。这不起作用:

<script type="text/javascript">
movesArray = new Array("s","s","s","s","s","s","s","s","s","s","s","s","s","s");
var pause = 100;
for (i=0; i<=14; i++)
{
    var t=setTimeout("ProcessKeypress(movesArray[i])", pause);
    pause = pause+100;
}
</script>

但是,如果我只是重复多次,它确实有效:

<script type="text/javascript">

var t=setTimeout("ProcessKeypress('s')", 100);
var t=setTimeout("ProcessKeypress('s')", 200);
var t=setTimeout("ProcessKeypress('s')", 300);
var t=setTimeout("ProcessKeypress('s')", 400);
var t=setTimeout("ProcessKeypress('s')", 500);

</script>

问题是,在某些情况下,我需要重复该功能几百次(可能更多),从而创建一个大网页。有没有办法循环遍历数组并延迟运行该函数?

Using Javascript, I'm trying to loop through an array and execute a function with a time delay on each loop. This does not work:

<script type="text/javascript">
movesArray = new Array("s","s","s","s","s","s","s","s","s","s","s","s","s","s");
var pause = 100;
for (i=0; i<=14; i++)
{
    var t=setTimeout("ProcessKeypress(movesArray[i])", pause);
    pause = pause+100;
}
</script>

However, if I just repeat it multiple times it does work:

<script type="text/javascript">

var t=setTimeout("ProcessKeypress('s')", 100);
var t=setTimeout("ProcessKeypress('s')", 200);
var t=setTimeout("ProcessKeypress('s')", 300);
var t=setTimeout("ProcessKeypress('s')", 400);
var t=setTimeout("ProcessKeypress('s')", 500);

</script>

The problem is that in some cases I will need to repeat the function a few hundred times (maybe more) which creates a large webpage. Is there a way to loop through the array and run the function on a time delay?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小…红帽 2024-12-27 16:41:36

您正在传递字符串“movesArray[i]”。只需转义字符串即可

movesArray = new Array("s","s","s","s","s","s","s","s","s","s","s","s","s","s");
var pause = 100;
for (i=0; i<=14; i++)
{
    var t=setTimeout("ProcessKeypress(movesArray[" +i +"])", pause);
    pause = pause+100;
}

You are passing the string "movesArray[i]". Just escape the string

movesArray = new Array("s","s","s","s","s","s","s","s","s","s","s","s","s","s");
var pause = 100;
for (i=0; i<=14; i++)
{
    var t=setTimeout("ProcessKeypress(movesArray[" +i +"])", pause);
    pause = pause+100;
}
病女 2024-12-27 16:41:36

使用setInterval()。您可以以任意时间间隔运行函数:

setInterval(function(){
    // do stuff
}, 300);

此外,请在 setTiemout 中使用 function(){} 而不仅仅是代码:

setTimeout(function(){
    // do stuff
}, pause);

Use setInterval(). You can run a function at any interval:

setInterval(function(){
    // do stuff
}, 300);

Also, use function(){} inside the setTiemout instead of just the code:

setTimeout(function(){
    // do stuff
}, pause);
梦中的蝴蝶 2024-12-27 16:41:36

如果您希望循环的每次迭代在前一次迭代完成后都有一定的时间延迟,那么您的执行方式将会在数组索引方面出现问题。这些是我看到的问题:

  1. 您的索引将不正确,因为对 ProcessKeypress() 的所有调用都会有 i == 15。
  2. 最好传递 setTimeout()实际的 javascript 函数而不是它必须计算的字符串。
  3. 而且,我还认为,通常最好在第一个计时器完成时设置下一个计时器,而不是立即设置所有计时器(尽管两者都可以工作)。
  4. 而且,您的代码已达到 movesArray[14],但数组中没有那么多项目。最好引用数组 .length 而不是硬编码 14

我的建议可以这样实施:

<script type="text/javascript">
movesArray = new Array("s","s","s","s","s","s","s","s","s","s","s","s","s","s");
var i = 0;

function nextIteration() {
    ProcessKeypress(movesArray[i++]);
    if (i < movesArray.length) {
        setTimeout(nextIteration, 100);
    }
}

nextIteration();

</script>

If you want each iteration of the loop to go a certain time delay after the previous one finishes, then your way of doing it will have an issues with the index into the array. These are the issues I see:

  1. Your index will not be correct as all calls to ProcessKeypress() will have i == 15.
  2. It's also better to pass setTimeout() an actual javascript function rather than a string that it has to evaluate.
  3. And, I also think it's generally better to just set the next timer when the first completes rather than set all of them at once (though either can work).
  4. And, your code was going up to movesArray[14], but you didn't have that many items in the array. It's better to refer to the array .length rather than hard code the 14.

My suggestions can be implemented like this:

<script type="text/javascript">
movesArray = new Array("s","s","s","s","s","s","s","s","s","s","s","s","s","s");
var i = 0;

function nextIteration() {
    ProcessKeypress(movesArray[i++]);
    if (i < movesArray.length) {
        setTimeout(nextIteration, 100);
    }
}

nextIteration();

</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文