How does setTimeout work in Node.JS?

后端 未结 5 1008
心在旅途
心在旅途 2020-11-30 19:35

I guess that once it\'s executed it\'s on the queue, but in the queue is there any assurance it will invoke exactly after X milliseconds? Or will other heavy tasks higher on

5条回答
  •  粉色の甜心
    2020-11-30 20:21

    setTimeout is a kind of Thread, it holds a operation for a given time and execute.

    setTimeout(function,time_in_mills);
    

    in here the first argument should be a function type; as an example if you want to print your name after 3 seconds, your code should be something like below.

    setTimeout(function(){console.log('your name')},3000);
    

    Key point to remember is, what ever you want to do by using the setTimeout method, do it inside a function. If you want to call some other method by parsing some parameters, your code should look like below:

    setTimeout(function(){yourOtherMethod(parameter);},3000);
    

提交回复
热议问题