使用setTimeout()调用函数 [英] Calling functions with setTimeout()

查看:626
本文介绍了使用setTimeout()调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单地说......

Simply put...

为什么

setTimeout('playNote('+currentaudio.id+', '+noteTime+')', delay);

工作正常,在指定的延迟后调用函数,但

work perfectly, calling the function after the the specified delay, but

setTimeout(playNote(currentaudio.id,noteTime), delay);

同时调用函数playNote?

calls the function playNote all at the same time?

(这些setTimeout()s在for循环中)

(these setTimeout()s are in a for loop)

或者,如果我的解释太难阅读,两个函数之间的区别是什么?

or, if my explanation is too hard to read, what is the difference between the two functions?

推荐答案

您列出的第一个表单有效,因为它将在结尾处评估一个字符串延迟。使用 eval()通常不是一个好主意,所以你应该避免这种情况。

The first form that you list works, since it will evaluate a string at the end of delay. Using eval() is generally not a good idea, so you should avoid this.

第二种方法没有工作,因为您立即使用 函数调用运算符<执行函数对象code>() 即可。最终发生的是,如果您使用 playNote(...)形式, playNote 会立即执行,所以没有将在延迟结束时发生。

The second method doesn't work, since you immediately execute a function object with the function call operator (). What ends up happening is that playNote is executed immediately if you use the form playNote(...), so nothing will happen at the end of the delay.

相反,你必须将匿名函数传递给setTimeout,所以正确的形式是:

Instead, you have to pass an anonymous function to setTimeout, so the correct form is:

setTimeout(function() { playNote(currentaudio.id,noteTime) }, delay);

请注意,您传递 setTimeout 整个函数表达式,因此它将保留匿名函数并仅在延迟结束时执行它。

Note that you are passing setTimeout an entire function expression, so it will hold on to the anonymous function and only execute it at the end of the delay.

您还可以传递 setTimeout 引用,因为引用不会立即执行,但是你不能传递参数:

You can also pass setTimeout a reference, since a reference isn't executed immediately, but then you can't pass arguments:

setTimeout(playNote, delay);






注意:

对于重复的事​​件,你可以使用 setInterval() ,您可以将 setInterval()设置为变量使用变量停止间隔 clearInterval()

For repeated events you can use setInterval() and you can set setInterval() to a variable and use the variable to stop the interval with clearInterval().

您说您使用 setTimeout()中用于循环。在许多情况下,最好在递归函数中使用 setTimeout()。这是因为在 for 循环中, setTimeout()中使用的变量将不是变量,因为它们是当 setTimeout()开始时,变量就像是在函数被触发时的延迟之后。

You say you use setTimeout() in a for loop. In many situations, it is better to use setTimeout() in a recursive function. This is because in a for loop, the variables used in the setTimeout() will not be the variables as they were when setTimeout() began, but the variables as they are after the delay when the function is fired.

Just使用递归函数来回避整个问题。

Just use a recursive function to sidestep this entire problem.

  // Set original delay
var delay = 500;

  // Call the function for the first time, to begin the recursion.
playNote(xxx, yyy);

  // The recursive function
function playNote(theId, theTime)
{
    // Do whatever has to be done
    // ...

    // Have the function call itself again after a delay, if necessary
    //   you can modify the arguments that you use here. As an
    //   example I add 20 to theTime each time. You can also modify
    //   the delay. I add 1/2 a second to the delay each time as an example.
    //   You can use a condition to continue or stop the recursion

    delay += 500;

    if (condition)
    { setTimeout(function() { playNote(theID, theTime + 20) }, delay); }
}

这篇关于使用setTimeout()调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆