Node.js and JS also have timers, timeout timers, interval timers, time timers, and Process.nexttick (callback) functions to implement event scheduling. Today, we'll learn the use of settimeout and setinterval.
First, settimeout timeout timer (and GCD in the after similar)
The Node.js built-in settimeout (Callback,delaymillseconds,[args) method can be used in node.js. When SetTime () is invoked, the callback function is delaymillseconds after
Execution. SetTime () returns a Timer object ID that can be canceled by passing the ID to cleartimeout (timeoutid) before the 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 will Cleartimeout (mytimeout), this line of comments can be seen after the execution of MyFunc ().
"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
Second, setinterval interval timer (similar to dispatch_source_t or Nstimer in GCD)
Interval timers are used to perform work at regular intervals. Similar to settimeout, Node.js built-in setinterval (Callback,delaymillisecond,[args) to create and return the Timer object ID, Canceled by Clearinterval ().
/**
* Created by the 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 code above is the callback function MyFunc that creates the setinterval, the parameter is interval,setinterval every 1s, settimeout is executed after 5 seconds, and its callback function lets the interval timer be canceled.
"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
Canceling the timer reference from the event loop
If you do not want to execute the timer callback function only in the event queue, you can use SetInterval and settimeout to return the unref () function of the object to notify the event loop not to continue.
When Unref () and settimeout are used in combination, to use a separate timer to wake up the event loop, a lot of use will have an impact on performance, should be used sparingly.
Iv. settimeout and setinterval execution times are imprecise.
They are added to the event queue for a certain amount of time, 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 results of more than one execution of the above code are also 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 to learn node.js in settimeout and setinterval of the use of methods to help.