Javascript Study Notes (15th) js call interval and time-out call

Source: Internet
Author: User

1. Timeout call setTimeout ()
The setTimeout () method accepts two parameters. The first parameter is the function, and the second parameter is the time (in microseconds). The value ID is returned.
Copy codeThe Code is as follows:
SetTimeout (function (){
Alert ("Hello! ");
},1000 );

Cancel clearTimeout () before calling and accept a parameter timeout call ID
Copy codeThe Code is as follows:
Var timeOutId = setTimeout (function (){
Alert ("Hello! ");
},1000 );

ClearTimeout (timeOutId );

2. Call setInterval () intermittently ()
The setInterval () method accepts two parameters. The first parameter is the function, and the second parameter is the time (in microseconds). The value ID is returned.
Copy codeThe Code is as follows:
SetInterval (function (){
Alert ("Hello! ");
},1000 );

Cancel call clearInterval () and accept a parameter's intermittent call ID
Copy codeThe Code is as follows:
Var intervalId = null;
Var span = document. createElement ("span"); // create a span Node
Span. Id = "time"; // you can specify the span id.
Document. body. appendChild (span); // Add span to the body
Function incrementNumber (){
Var now = new Date ();
Var timeStr = now. toLocaleTimeString ();
Span. innerText = timeStr;
Num ++;
If (num = 10 ){
ClearInterval (intervalId); // The time remains unchanged after 10 seconds
}
}
IntervalId = setInterval (incrementNumber, 1000 );

3. Try to replace intermittent calls with time-out calls
Copy codeThe Code is as follows:
Var num = 0;
Var max = 10;
Function incrementNumber (){
Num ++;
If (num <max ){
SetTimeout (incrementNumber, 1000 );
} Else {
Alert ("OK ");
}
}
SetTimeout (incrementNumber, 1000 );

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.