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

高洛峰
Release: 2017-02-08 15:54:26
Original
1196 people have browsed it

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 setTime() is called, the callback function will be executed

after delayMillSeconds. 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);
Copy after login
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe timer.js Process finished with exit code 0
Copy after login

If you comment 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
Copy after login

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

The interval timer is used to perform work at regular intervals. And setTimeout Similarly, 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);
Copy after login

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 sets the interval timer. Cancel.

"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
Copy after login

3. Cancel the timer reference from the event loop

When there are only timer callback functions in the event queue, if you do not want to execute them anymore, you can use setInterval and setTimeout return the object's unref() function 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 not precise

They add callbacks to the event queue at a certain interval, 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");
Copy after login

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
Copy after login

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.

For more related articles on how to quickly master setTimeout and setInterval in Node.js, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!