Javascript learning notes (15th) js call interval and time-out call _ basic knowledge

Source: Internet
Author: User
Introduction to js intermittent call and timeout call in javascript learning notes. For details about js learning, refer to section 1. setTimeout () for timeout call ()
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.

The Code is as follows:


SetTimeout (function (){
Alert ("Hello! ");
},1000 );


Cancel clearTimeout () before calling and accept a parameter timeout call ID

The 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.

The Code is as follows:


SetInterval (function (){
Alert ("Hello! ");
},1000 );


Cancel call clearInterval () and accept a parameter's intermittent call ID

The 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

The Code is as follows:


Var num = 0;
Var max = 10;
Function incrementNumber (){
Num ++;
If (num 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.