JavaScript Learning Notes (15) JS Intermittent Call and Timeout call _ basics

Source: Internet
Author: User
Tags setinterval
1. Timeout call settimeout ()
The settimeout () method accepts two parameters, the first parameter is a function, the second parameter is the time (in microseconds), and returns the value ID
Copy Code code as follows:

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

Cancels cleartimeout () before calling to accept a parameter timeout call ID
Copy Code code as follows:

var Timeoutid = settimeout (function () {
Alert ("Hello!");
},1000);

Cleartimeout (Timeoutid);

2. Intermittent call SetInterval ()
The SetInterval () method accepts two parameters, the first parameter is a function, the second parameter is the time (in microseconds), and returns the value ID
Copy Code code as follows:

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

Cancels calling Clearinterval (), accepting a parameter intermittent call ID
Copy Code code as follows:

var intervalid = null;
var span = document.createelement ("span"); To create a span node
Span. Id= "Time"; Set the ID of span
Document.body.appendChild (span); Add span within body
function Incrementnumber () {
var now = new Date ();
var timestr = now.tolocaletimestring ();
Span.innertext =timestr;
num++;
if (num = 10) {
Clearinterval (Intervalid); In 10 seconds, the time is unchanged.
}
}
Intervalid = SetInterval (incrementnumber,1000);

3. Try to use timeout call instead of intermittent call
Copy Code code as follows:

var num = 0;
var max = 10;
Function Incrementnumber () {
num++;
if (num<max) {
settimeout (incrementnumber,1000);
} else {
alert ("OK");
}
}
settimeout (incrementnumber,1000);

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.