Home  >  Article  >  Web Front-end  >  Quickly master the use of setTimeout and setInterval in Node.js_node.js

Quickly master the use of setTimeout and setInterval in Node.js_node.js

WBOY
WBOYOriginal
2016-05-16 15:09:561487browse

Node.js, like js, also has timers, timeout timers, interval timers, timely timers, and the process.nextTick(callback) function to implement event scheduling. Today, let’s learn how to use setTimeout and setInterval.

1. setTimeout timeout timer (similar to after in GCD)

In node.js, you can use the node.js built-in setTimeout(callback, delayMillSeconds, [args]) method. When calling setTime(), the callback function will be after delayMillSeconds

Executing .setTime() will return a timer object ID, which can be canceled by passing the ID to clearTimeout(timeoutId) before delayMillSeconds expires.

function myfunc(){
 console.log("myfunc");
};
var mytimeout=setTimeout(myfunc,1000);
clearTimeout(mytimeout);
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe timer.js

Process finished with exit code 0

If you comment out the clearTimeout(mytimeout); line, you can see that myfunc() will be executed.

"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe timer.js
myfunc

Process finished with exit code 0

2. setInterval interval timer (similar to dispatch_source_t or NSTimer in GCD)

Interval timer is used to perform work at regular intervals. Similar to setTimeout, node.js has built-in setInterval(callback, delayMilliSecond, [args]) to create and return the timer object ID, and cancel it through clearInterval() .

/**
 * Created by Administrator on 2016/3/11.
 */
function myfunc(Interval){
 console.log("myfunc "+Interval);
}
var myInterval=setInterval(myfunc,1000,"Interval");
function stopInterval(){
 clearTimeout(myInterval);
 //myInterval.unref();
}
setTimeout(stopInterval,5000);

The above code is to create the callback function myfunc of setInterval. The parameter is Interval. setInterval is executed every 1s. setTimeout is executed after 5 seconds. Its callback function cancels the interval timer.

"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe Interval.js
myfunc Interval
myfunc Interval
myfunc Interval
myfunc Interval

Process finished with exit code 0

3. Cancel the timer reference from the event loop

When there are only timer callback functions in the event queue, if you no longer want to execute them, you can use the unref() function of the object returned by setInterval and setTimeout to notify the event loop not to continue.

When unref() is used in combination with setTimeout, an independent timer must be used to wake up the event loop. Extensive use will also have an impact on performance, so use it as little as possible.

4. The execution time of setTimeout and setInterval is inaccurate

They add callbacks to the event queue at certain intervals, and the execution is not too precise

function simpleTimeout(consoleTime)
{
 console.timeEnd(consoleTime);
}
console.time("twoSecond");
setTimeout(simpleTimeout,2000,"twoSecond");

console.time("oneSecond");
setTimeout(simpleTimeout,1000,"oneSecond");

console.time("fiveSecond");
setTimeout(simpleTimeout,5000,"fiveSecond");

console.time("50MillSecond");
setTimeout(simpleTimeout,50,"50MillSecond");

The output results of executing the above code several times will be different.

"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe timer.js
50MillSecond: 51ms
oneSecond: 1000ms
twoSecond: 2002ms
fiveSecond: 5001ms

Process finished with exit code 0

The above is the entire content of this article. I hope it will be helpful for everyone to learn how to use setTimeout and setInterval in Node.js.

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