Nodejs minimalism tutorial (ii) : timer

  • 2020-03-30 04:10:12
  • OfStack

SetTimeout and clearTimeout


var obj = setTimeout(cb, ms);

SetTimeout is used to set a callback function, cb, to be executed in at least ms milliseconds (not immediately after ms milliseconds). The setTimeout return value can be used as an argument to clearTimeout, which is used to stop the timer so that the callback function is not executed.

SetInterval and clearInterval


var obj = setInterval(cb, ms);

SetInterval is similar to setTimeout, except that setInterval executes cb every ms milliseconds (not exactly ms milliseconds). The setInterval return value can be used as an argument to clearInterval, which is used to stop the timer so that the callback function is not executed.

SetImmediate and clearImmediate


var obj = setImmediate(cb);

Cb function called setImmediate used to delay. Cb will be called after the I/O event callback and before the setTimeout and setInterval callbacks. SetImmediate return values can be used as the parameter, clearImmediate clearImmediate used to trigger the callback function to stop.

Process. NextTick


process.nextTick(cb);

Cb function called similar setImmediate function, used to delay. Cb will call before I/O event callback (different from setImmediate). Process.nexttick is much more efficient than setTimeout(cb, 0). Process. NextTick in each cycle will perform the process. The most maxTickDepth a callback function, and setImmediate in each loop only perform a callback function.


Related articles: