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

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

问题描述

简单地说...

为什么

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() 在 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?

推荐答案

您列出的第一个表单有效,因为它将在 delay 的末尾计算一个字符串.使用 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.

第二种方法不起作用,因为您立即使用 函数调用运算符().最终发生的是,如果您使用 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().

您说您在 for 循环中使用了 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 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天全站免登陆