简体   繁体   English

Node.js中的setTimeout细微差别

[英]setTimeout nuances in Node.js

I'm trying to understand how the callback function works inside the setTimeout function. 我试图了解setTimeout函数中回调函数的工作方式。 I'm aware the format is: setTimeout(callback, delay) I wrote a little test script to explore this. 我知道格式是: setTimeout(callback, delay)我写了一些测试脚本来探索这一点。

test1.js test1.js

console.log("Hello")

setTimeout(function () { console.log("Goodbye!") }, 5000)

console.log("Non-blocking")

This works as expected, printing Hello <CRLF> Non-blocking and then 5 seconds later, prints Goodbye! 这将按预期工作,打印Hello <CRLF> Non-blocking ,然后5秒钟后打印Goodbye!

I then wanted to bring the function outside of the setTimeout like this: 然后,我想将函数带到setTimeout之外,如下所示:

console.log("Hello")

setTimeout(goodbye(), 5000)

console.log("Non-blocking")

function goodbye () {
    console.log("Goodbye")
}

but it doesn't work and there isn't a 5 second delay between Non-blocking and Goodbye! 但它不起作用,并且“ Non-blocking和“ Goodbye!之间没有5秒的延迟Goodbye! , they print straight after each other. ,它们彼此直接打印。

It works if I remove the brackets from the function call in the timeout, like this: 如果我在超时中从函数调用中删除了括号,它会起作用,如下所示:

setTimeout(goodbye, 5000)

but this doesn't make sense to me because that's not how you call a function. 但这对我来说没有意义,因为那不是您调用函数的方式。 Futhermore, how would you pass arguments to the function if it looked like this?! 此外,如果它看起来像这样,如何将参数传递给该函数?

var name = "Adam"    

console.log("Hello")

setTimeout(goodbye(name), 5000)

console.log("Non-blocking")

function goodbye (name) {
    console.log("Goodbye "+name)
}

My question is really, why doesn't it work when there are parameters in the function, despite the fact the setTimeout is being provided with a valid function with the correct syntax? 我的问题确实是,尽管setTimeout提供了具有正确语法的有效函数,但在函数中有参数时为什么它不起作用?

By putting the parentheses after your function name, you are effectively calling it, and not passing the function as a callback. 通过在函数名称后面加上括号,可以有效地调用它,而不会将函数作为回调传递。

To provide parameters to the function you are calling: 为要调用的函数提供参数:

  1. You can pass an anon function. 您可以传递anon函数。 setTimeout(function(){goodbye(name)}, 5000);
  2. Or, you can pass the arguments as a third parameter. 或者,您可以将参数作为第三个参数传递。 setTimeout(goodbye, 5000, name);

Look at this question: How can I pass a parameter to a setTimeout() callback? 看一下这个问题: 如何将参数传递给setTimeout()回调?

No matter where you place it, goodbye(name) executes the function immediately . 无论放置在何处, goodbye(name)立即执行该功能。 So you should instead pass the function itself to setTimeout() : setTimeout(goodbye, 5000, name) . 因此,您应该将函数本身传递给setTimeout()setTimeout(goodbye, 5000, name)

When you use it like this: 当您像这样使用它时:

setTimeout(goodbye(), 5000);

it will first call goodbye to get its return value, then it will call setTimeout using the returned value. 它将首先调用goodbye以获得其返回值,然后将使用返回的值调用setTimeout

You should call setTimeout with a reference to a callback function, ie only specifying the name of the function so that you get its reference instead of calling it: 您应该使用对回调函数的引用来调用setTimeout ,即仅指定函数的名称,以便获得其引用而不是调用它:

setTimeout(goodbye, 5000);

To make a function reference when you want to send a parameter to the callback function, you can wrap it in a function expression: 要在向回调函数发送参数时引用函数,可以将其包装在函数表达式中:

setTimeout(function() { goodbye(name); }, 5000);

You can use parantheses in the call, but then the function should return a function reference to the actual callback function: 可以在调用中使用括号,但是该函数应返回对实际回调函数的函数引用:

setTimeout(createCallback(), 5000);

function createCallback() {
  return function() {
    console.log("Goodbye");
  };
}

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

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