Difference between setTimeout and setinterval functions in Javascript

Source: Internet
Author: User
From a few years ago (more than five years) until now, I have read the setTimeout and setinterval functions in many documents on JavaScript, they always think that their functions are completely the same-that is, how long it takes to execute something, so they always use the setTimeout function and are always in their own Code I have never used the setinterval function. In fact, why don't I think about it further? If the two functions are the same, why is there a rational existence.
So far, I am used to expressing doubts about the consistency of the two functions. I just suddenly realized that I had to Google them online. There was a difference. First of all, we can understand the two words as follows:
Timeout
Interval: interval, distance, off-screen, interval
The time specified by the setTimeout function table they represent is the time to execute a specified function. At this time, its mission is to complete and only be executed once. The setinterval function executes the specified function at a specified time. If it is not cleared, it will be executed countless times. The setinterval function acts more like a lot. Programming Language The behavior of the timer control.
The clearing counter functions are cleartimeout and clearinterval. The functions are described as follows:
VaR timer1 = setTimeout ("foo", 1000); // set the foo function to be executed once in 1000 milliseconds
...... // Do something
Cleartimeout (timer1); // If Program Run here, when the count starts with setTimeout, 1000 milliseconds fail,
// After cleartimeout, foo cannot be executed
// IF 1000 milliseconds have passed and foo has been triggered for execution, it makes no sense.
Vartimer2 = setinterval ("foo", 1000); // set to execute Foo once every 1000 milliseconds from the current time.
...... // Do something
Clearinterval (timer2); // Foo will not be triggered later
So such a detail is ignored, and setTimeout + recursion has been used for a long time to simulate setinterval behavior. The following code:
Function Foo (){
... // Do something
SetTimeout ("foo", 1000 );
}
Does the above Code look the same as setinterval? It just seems that there are three lines of code, and it doesn't seem to be much complicated. But why don't we use something simple?
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.