Javascript setTimeout 回调参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5520155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setTimeout callback argument
提问by jsnewman
Let's consider this piece of JavaScript:
让我们考虑一下这段 JavaScript:
function Person(name) {
this.name = name;
}
Person.prototype.showName = function() {
alert(this.name);
}
var mike = new Person("mike");
//mike.showName();
window.name = "window";
I don't understand the difference between the behavior of
我不明白行为之间的区别
setTimeout(mike.showName(), 5000);
and
和
setTimeout(function(){
mike.showName();
}, 5000);
Why is the behavior different? It really confuses me. Thanks.
为什么行为不同?这真的让我很困惑。谢谢。
回答by Wayne
Your question really has nothing at allto do with setTimeout
. You simply need to understand the difference between a function call and a reference to a function.
你的问题真的有什么都没有做setTimeout
。您只需要了解函数调用和对函数的引用之间的区别。
Consider these four assignments:
考虑这四个任务:
var one = function() { mike.showName(); };
var two = mike.showName;
var three = mike.showName();
var four = (function() { mike.showName(); })();
The first two assign a reference to a function to their respective variables. The last two, however, callfunctions (that's what the parens are for) and assign their return values to the vars on the left-hand side.
前两个将函数的引用分配给它们各自的变量。然而,最后两个调用函数(这就是括号的用途)并将它们的返回值分配给左侧的 vars。
How this relates to setTimeout:
这与 setTimeout 有何关系:
The setTimeout
function expects as its first argument a referenceto a function, so either one
or two
above would be correct, but three
and four
would not. However, it is important to note that it is not, strictly speaking, a mistaketo pass the return value of a function to setTimeout
, although you'll frequently see that said.
的setTimeout
函数期望作为第一个参数一个参考到的函数,因此任一one
或two
以上将是正确,但three
并four
不会。但是,重要的是要注意,严格来说,将函数的返回值传递给并不是错误setTimeout
,尽管您会经常看到这样说。
This is perfectly fine, for example:
这完全没问题,例如:
function makeTimeoutFunc(param) {
return function() {
// does something with param
}
}
setTimeout(makeTimeoutFunc(), 5000);
It has nothing to do with howsetTimeout
receives a function as its argument, but that it does.
它与如何setTimeout
接收函数作为其参数无关,但它确实。
回答by JCOC611
If the accepted answer is just too long to read:
如果接受的答案太长而无法阅读:
setTimeout(mike.showName(), 5000);
This will execute whatever mike.showName()
returnsafter 5,000 milliseconds.
这将执行5,000 毫秒后mike.showName()
返回的任何内容。
setTimeout(function(){ mike.showName(); }, 5000);
This will execute anonymous function after 5000 milliseconds that calls mike.showName()
, the actual function.
这将后5000毫秒调用执行匿名函数mike.showName()
,实际功能。
Another way to achieve same effect:
实现相同效果的另一种方法:
setTimeout(mike.showName.bind(mike), 5000);
回答by ThiefMaster
It's not a performance issue. One of the ways you showed simply doesn't work (it calls the function immediately instead of when the timeout fires).
这不是性能问题。您展示的其中一种方法根本不起作用(它立即调用该函数,而不是在超时触发时调用)。
setTimeout(mike.showName(), 5000);
will execute the showName
function and sets its return value as the timeout callback which won't work.
setTimeout(mike.showName(), 5000);
将执行该showName
函数并将其返回值设置为不起作用的超时回调。
setTimeout(function(){ mike.showName(); }, 5000);
creates an anonymous function and sets this as the timeout callback. When the timeout fires, the function is called and calls your showName()
function.
setTimeout(function(){ mike.showName(); }, 5000);
创建一个匿名函数并将其设置为超时回调。当超时触发时,将调用该函数并调用您的showName()
函数。
Fyi, setTimeout('mike.showName();', 5000);
would also work. But don't do that- it's just as bad as using eval()
. Besides that it makes your code less readable since the code in the string cannot be syntax-highlighted.
仅供参考,setTimeout('mike.showName();', 5000);
也可以。但是不要那样做——这和使用eval()
. 除此之外,它使您的代码可读性降低,因为字符串中的代码不能以语法高亮显示。
回答by Mike Park
setTimeout(mike.showName(), 5000);
executes mike.showName()
immediately and passes the return value to setTimeout()
setTimeout(mike.showName(), 5000);
mike.showName()
立即执行并将返回值传递给setTimeout()
setTimeout(function(){ mike.showName(); }, 5000);
passes a pointer to the function instead. That way setTimeout
can execute the function, rather than it's return value.
setTimeout(function(){ mike.showName(); }, 5000);
而是传递一个指向该函数的指针。这样setTimeout
就可以执行函数,而不是它的返回值。