Home  >  Article  >  Web Front-end  >  Nodejs minimalist introductory tutorial (2): timer_node.js

Nodejs minimalist introductory tutorial (2): timer_node.js

WBOY
WBOYOriginal
2016-05-16 16:32:511415browse

setTimeout and clearTimeout

Copy code The code is as follows:

var obj = setTimeout(cb, ms);

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

setInterval and clearInterval

Copy code The code is as follows:

var obj = setInterval(cb, ms);

setInterval is similar to setTimeout, but setInterval will execute cb every ms milliseconds (not exactly ms milliseconds). The return value of setInterval can be used as a parameter of clearInterval, which is used to stop the timer so that the callback function will not be executed.

setImmediate and clearImmediate

Copy code The code is as follows:

var obj = setImmediate(cb);

setImmediate is used to delay calling the cb function. cb will be called after the I/O event callback and before the setTimeout and setInterval callbacks. The return value of setImmediate can be used as a parameter of clearImmediate, which is used to stop triggering the callback function.

process.nextTick

Copy code The code is as follows:

process.nextTick(cb);

Similar to the setImmediate function, used to delay calling the cb function. cb will be called before the I/O event callback (different from setImmediate). process.nextTick is much more efficient than setTimeout(cb, 0). process.nextTick will execute up to process.maxTickDepth callback functions in each loop, while setImmediate will only execute one callback function in each loop.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn