This article describes how to quickly master Node. the use of setTimeout and setInterval in js. If you are interested, refer to Node. js and js also have timers, time-out timers, interval timers, timely timers, and processes. nextTick (callback) function to implement event scheduling. Learn how to use setTimeout and setInterval today.
1. setTimeout timeout timer (similar to after in GCD)
You can use the setTimeout (callback, delayMillSeconds, [args]) Method built in node. js. When setTime () is called, The callback function will be after delayMillSeconds
Execution. setTime () returns a timer Object ID. You can send the ID to clearTimeout (timeoutId) before the expiration of delayMillSeconds to cancel the operation.
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.jsProcess finished with exit code 0
If clearTimeout (mytimeout) is added, myfunc () is executed ().
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe timer.jsmyfuncProcess finished with exit code 0
Ii. setInterval interval Timer(Similar to dispatch_source_t or NSTimer in GCD)
The interval timer is used to perform tasks at regular intervals. similar to setTimeout, node. built-in setInterval (callback, delayMilliSecond, [args]) in js to create and return the timer Object Id, which is canceled 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 creates the setInterval callback function myfunc. The parameter is Interval. setInterval is executed every 1 S. 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.jsmyfunc Intervalmyfunc Intervalmyfunc Intervalmyfunc IntervalProcess finished with exit code 0
3. Cancel timer reference from the event Loop
When only timer callback functions exist in the event queue, if you do not want to execute them, you can use setInterval and setTimeout to return the unref () Functions of the object to notify the event loop not to continue.
When unref () and setTimeout are used in combination, an independent timer is used to wake up the event loop. A large amount of use will also affect the performance, and should be used as little as possible.
IV. The execution time of setTimeout and setInterval is inaccurate.
They add the callback 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");
The output results of the above Code are different after multiple executions.
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe timer.js50MillSecond: 51msoneSecond: 1000mstwoSecond: 2002msfiveSecond: 5001msProcess finished with exit code 0
The above is all the content in this article. I hope it will be helpful for you to learn how to use setTimeout and setInterval in Node. js.