在JavaScript中使用setTimeout()和setInterval()时调用函数 [英] Calling a function when using setTimeout() and setInterval() in JavaScript
问题描述
如果我使用setTimeout()和setInterval()调用命名函数而没有括号,则它按预期工作。当我用括号调用相同的函数时,它会立即执行它或者给出错误。
If I call a named function using setTimeout() and setInterval() without the parentheses it works as expected. When I call the same function with the parentheses it either executes it immediately or gives an error.
我正在寻找一个更深入的了解这个问题然后我发现了什么网络。
你能告诉我为什么这是真的吗?
I am looking for a deeper understanding in this matter then what I have found on the web. Would you guys please explain to me why this is true?
var func = function(){
console.log("Bowties are cool.");
}
setTimeout(func(), 1500);
// Prints "Bowties are cool." immediately
setInterval(func(), 1500);
// Throws an error
setInterval(func, 1500);
// Works as expected
setTimeout(console.log("Bowties are cool."),1500);
// This method has the same result as "setTimeout(func(), 1500)".
推荐答案
您必须将函数引用传递给 setTimeout()
和 setInterval()
。这意味着你传递一个没有()
的函数名,或者你传递一个匿名函数。
You must pass a function reference to both setTimeout()
and setInterval()
. That means you pass a function name without the ()
after it or you pass an anonymous function.
当你在函数名之后包含()
,如 func()
,你正在执行该函数,然后传递将结果返回 setInterval()
或返回 setTimeout()
。除非函数本身返回另一个函数引用,否则它将永远不会执行您想要的操作。对于Javascript程序员来说这是一个非常常见的错误(我在学习语言时犯了同样的错误)。
When you include the ()
after the function name as in func()
, you are executing the function immediately and then passing the return result to setInterval()
or to setTimeout()
. Unless the function itself returns another function reference, this will never do what you want. This is a very common mistake for Javascript programmers (I made the same mistake myself when learning the language).
所以,正确的代码是:
setTimeout(func, 1500);
setInterval(func, 1500);
如果你知道其他语言使用指针,你可以想到一个函数的名称与()
之后它就像一个指向函数的指针,这就是Javascript中的函数引用,当你希望它能够执行某些函数时,你传递给函数的是什么功能稍后,而不是立即。
If you know other languages that use pointers, you can think of the name of a function with the ()
after it as like a pointer to the function and that's what a function reference is in Javascript and that's what you pass to a function when you want it to be able to execute some function later, not immediately.
当你错误地这样做时:
setTimeout(func(), 1500);
setInterval(func(), 1500);
它正在立即执行你的 func()
然后将返回结果传递给 setTimeout()
和 setInterval()
。由于你的 func()
没有返回任何内容,你基本上是这样做的:
It is executing your func()
immediately and then passing the return result to setTimeout()
and setInterval()
. Since your func()
doesn't return anything, you are essentially doing this:
func();
setTimeout(undefined, 1500);
您观察到的是什么,但不是您想要的。
Which is what you observe happening, but not what you want.
此外,如果你想执行特定的函数调用,例如 console.log(Bowties很酷。)
,然后你可以将它包装在另一个函数中:
Further, if you want to execute a specific function call such as console.log("Bowties are cool.")
, then you can wrap it in another function like this:
setTimeout(function() {
console.log("Bowties are cool.")
}, 1500);
因此,您再次将函数引用传递给 setTimeout()
可以执行,而不是立即执行,这就是你正在做的事情。
So, again you are passing a function reference to setTimeout()
that can be executed LATER rather than executing it immediately which is what you were doing.
这篇关于在JavaScript中使用setTimeout()和setInterval()时调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!