/*
* Global functions and methods
* 1, SetTimeout, cleartimeout How long after the function is executed (only once)
* 2, SetInterval, clearinterval the timer that executes once every few seconds
* 3, Unref,ref stop and start timer (for setinterval)
*/
1, SetTimeout, cleartimeout
A. Specifying how long to execute functions and clear functions
function timer () {
Console.log ("This was loading after 500S.");
}
SetTimeout (timer,500);
B. Arguments can be passed to the called function (Note: settimeout can pass multiple arguments to the function)
function Sum1 (str,num1,num2) {
Console.log (str+ (num1+num2) + ".");
}
The first value after the SetTimeout function is the time of the calculator, followed by the arguments passed to the function
SetTimeout (sum1,1000, "the number of both and is equal to:", 5000,8000);
C. Clear Timings
function timer1 (num) {
Console.log ("This was loading after" +num+ "s.");
}
var ctimer= setTimeout (timer1,5000);
Cleartimeout (Ctimer);
2, Setinterval,clearinterval the timer that executes once every few seconds
function Intervalf (num) {
Console.log ("The time of SetInterval load is" +num);
}
Execute once per 1000S
var interval=setinterval (intervalf,1000,1000);
Section 3000s is the purge timer
SetTimeout (function () {
Clearinterval (interval);
},3000);
3. Unref,ref Stop and start timer
function intervalF1 (num) {
Console.log ("The time of SetInterval load is" +num);
}
Execute once per 1000S
var interval1=setinterval (intervalf1,1000,1000);
A.unref Stop timer, such as: Stop timer at 3000s
SetTimeout (function () {
Interval1.unref ();
Console.log ("3000s stop interval1 of SetInterval.");
},3000);
B.ref start timer, such as: Start timer at 1000s
SetTimeout (function () {
Interval1.ref ();
Console.log ("10000s start Interval1 of setinterval.");
},10000);
The global function in Nodejs settimeout/cleartimeout,setinterval/clearinterval,unref/ref