Home >Web Front-end >JS Tutorial >Nodejs minimalist introductory tutorial (2): timer_node.js
setTimeout and clearTimeout
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
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
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
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.