SetTimeout () and setinterval () Timer and Difference

Source: Internet
Author: User
Tags setinterval
The code is as follows Copy Code

settimeout (function () {
Alert (' Hello! ');
}, 0);
SetInterval (callbackfunction, 100);

It is assumed that the greeting method in settimeout is executed immediately, because it is not a figment of the imagination, but rather a JavaScript API document that explicitly defines how many milliseconds the second parameter meaning is, and the callback method is executed. This is set to 0 milliseconds, and is taken for granted immediately.
The same callbackfunction method for SetInterval is executed immediately at intervals of 100 milliseconds. Believe it!

But with the growing and enriching experience of JavaScript application development, one day you find a strange piece of code that you don't understand:

The code is as follows Copy Code

Div.onclick = function () {
settimeout (function () {
document.getElementById (' Inputfield '). focus ();
}, 0);
};

Since it is 0 milliseconds to execute, then what else to do with settimeout, at the moment, the firm belief has begun to waver.

The difference between settimeout () and SetInterval () method

Timer settimeout () and setinterval () are the most important and useful two methods of native JS, but many people have been mistaken for the same function: Interval time repeatedly executes the incoming handle function. But in fact, not so, since JS gives two different names, there must be a difference.
Let's take a look at both the JS manual and the English Dictionary explanation:
JS manual»settimeout (): Used to call a function or evaluate an expression after a specified number of milliseconds;
English explanation»timeout (): timeout, temporary rest, breaks;
JS manual»setinterval (): Invokes a function or evaluation expression in the specified period (in milliseconds). The function is called continuously until the Clearinterval () is called or the window is closed;
English explanation»interval (): interval; spacing; intermission;
It is not difficult to see, as long as we carefully understand the JS manual and the name of interpretation, can be very easy to separate the difference between the two. To put it simply, the difference between the two is that the settimeout () method executes the function after waiting for a specified time and executes only one incoming handle function. The SetInterval () method executes an incoming handle function after each specified interval, looping until the window or clearinterval () is closed.

Cases

  code is as follows copy code

var timeout=function () {
 alert (' Wait until 2s pops up, just once! Cleartimeout can be stopped during waiting time! ')
}
var interval=function () {
 alert (' Eject every 2s loop until clearinterval or close the window! ')
}
Var input=document.getelementsbytagname (' input ');

Var cleartimeoutfun=null;
Var clearintervalfun=null;

Input[0].onclick=function () {
 cleartimeoutfun=settimeout (timeout,2000);
}
Input[1].onclick=function () {
 cleartimeout (cleartimeoutfun);
}
Input[2].onclick=function () {
 clearintervalfun=setinterval (interval,2000);
}
Input[3].onclick=function () {
 clearinterval (clearintervalfun);
}  

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.