简体   繁体   English

setTimeout如何工作?

[英]How setTimeout works?

I have a concern with setTimeout function in javascript. 我担心javascript中的setTimeout函数。 when we call setTimeout function without return anything, it is okay for me. 当我们调用setTimeout函数而不返回任何内容时,对我来说还可以。 like 喜欢

setTimeout(function() {
   console.log("ok function called")
},2000); 

here in the above example it just simply call that function after 2000ms, 在上面的示例中,它只是在2000ms之后简单地调用该函数,

And if I write this like 如果我这样写

setTimeout(function(params) {
   console.log("passed value is"+params)
},2000,55);

now it will call this function with 55 as an argument, right? 现在它将以55作为参数调用此函数,对吗?

But problem is that when I call to write this like 但是问题是当我打电话写这样的时候

setTimeout(function(params) {
   console.log("passed value is"+params)
}(55),2000);

here function is calling with 55 as params but it is now waiting for 2000ms 这里函数以55作为参数调用,但是现在等待2000ms

And when I wrote like 当我写像

setTimeout(function(params) {
    console.log("passed value is "+params);
     return function(){
      console.log(params)
     };
 }(55),2000);

in this only return function is calling with 2000ms delay, the line console.log("passed value is "+params); 在此唯一的返回函数以2000ms延迟调用的情况下,console.log(“ passed value is” + params); is executing instantly 立即执行

please help me get out of this problem. 请帮助我摆脱这个问题。

One is a function. 一个是功能。 Another is a function call. 另一个是函数调用。

First, let's forget javascript for now. 首先,让我们暂时忘记javascript。 If you know any other programming language, what do you expect the two pieces of code below to do? 如果您知道任何其他编程语言,那么您期望下面的两段代码做什么?

function a () { return 1 }

x = a;
y = a();

What do you expect x to be? 您期望x是什么? 1 or a pointer to function a ? 1或函数指针a

What do you expect y to be? 您期望y是什么? 1 or a pointer to function a ? 1或函数指针a

A function is not a function call. 函数不是函数调用。 When you call a function it returns a value. 调用函数时,它将返回一个值。


Now let's switch back to javascript. 现在,让我们切换回javascript。 Whenever I get confused by a piece of code, I try to make the syntax simpler so that I can understand what's going on: 每当我对一段代码感到困惑时,我都会尝试使语法更简单,以便我理解发生了什么:

setTimeout(function() {console.log("ok function called")}, 2000);

Now, that's a compact piece of code, let's make the syntax simpler. 现在,这是一段紧凑的代码,让我们简化语法。 The above code is the same as: 上面的代码与:

var a = function() {console.log("ok function called")};
setTimeout(a, 2000);

So what does that do? 那怎么办? It will call the function a after 2 seconds. 2秒后它将调用函数a

Now let's take a look at: 现在让我们看一下:

setTimeout(function() {console.log("ok function called")}(), 2000);
                                  // Note this ----------^^

That's the same as: 等同于:

 var b = function() {console.log("ok function called")}();
 setTimeout(b, 2000);

which can further be simplified to: 可以进一步简化为:

var a = function() {console.log("ok function called")};
var b = a();
setTimeout(b, 2000);

So I hope you see what you're really passing to setTimeout. 因此,我希望您看到真正传递给setTimeout的内容。 You're passing the return value of the function, not the function. 您传递的是函数的返回值,而不是函数。

When you write 当你写

setTimeout(function (params) { return something; }(55), 2000);

what actually happens is something like this: 实际发生的情况是这样的:

var _temp_func = function (params) { return something; };
var _temp = _temp_func(55);
setTimeout(_temp, 2000);

The anonymous function you have as a parameter to setTimeout is evaluated immediately, even before the call to setTimeout itself. 您拥有作为setTimeout参数的匿名函数,即使在调用setTimeout本身之前,也会立即进行评估。 In contrast to that, the actual parameter that ends up in _temp here is called with a delay. 与此相反,在此处以_temp结尾的实际参数被延迟调用。 This is what happens in your last example. 这就是您上一个示例中发生的情况。

setTimeout takes only function name without parenthesis. setTimeout仅使用函数名称,不带括号。
correct syntax : setTimeout(Helloworld) - here you are setting function 正确的语法:setTimeout(Helloworld)-在这里设置函数
incorrect syntax : setTimeout(HelloWorld()) - here you are calling function 错误的语法:setTimeout(HelloWorld())-在这里调用函数

or non IIFE function. 或非IIFE功能。 It's an IIFE that you are passing hence it is getting called immediately. 您正在通过的是IIFE,因此会立即被调用。 setTimeout(function (params) { return something; }(55), 2000); setTimeout(function(params){return something;}(55),2000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM