The global function in Nodejs settimeout/cleartimeout,setinterval/clearinterval,unref/ref

Source: Internet
Author: User

/*
* 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.