Difference between the setTimeout () and setinterval () Methods of JS timers

Source: Internet
Author: User

Timer setTimeout () and setinterval () are two very important and useful methods of native Js. However, many people mistakenly think that they are using the same function: Execute the incoming handle function repeatedly at intervals. but in fact, this is not the case. Since JS provides two different names, there must be some differences.
Let's take a look at the two JS manuals and their explanations in the English dictionary:
JS manual» setTimeout (): used to call a function or computing expression after a specified number of milliseconds;
» Timeout (): Time-out; temporary break; Inter-work break;
JS manual» setinterval (): Call a function or computing expression according to the specified period (in milliseconds). The function will be called continuously until clearinterval () is called or the window is closed;
» Interval (): interval; spacing; interscreen rest;
It is not hard to see that as long as we carefully understand the JS manual and naming definitions, we can easily distinguish the two. Simply put, the difference between the two lies in,The setTimeout () method is to execute the function after a specified time and only execute the incoming handle function once. the setinterval () method is to execute the incoming handle function once after each specified interval, and execute it cyclically until the window is closed or clearinterval ().
To make it easier for obfuscators to understand, I wrote an example specifically:Click here to view the demo
As for usage, you don't have to say much. After understanding the differences between the two, you should know how to choose between them.

@ Popular Article
    • Recruit part-time web front-end developers 1 ~ 2 [currently available]
    • Details about jquery queue control methods: Queue ()/dequeue ()/clearqueue ()
    • Structure/performance/behavior separation tab (JQ and native JS)
    • Difference, not a little bit
    • Abbreviated getelementsbytagname
Clearinterval, cleartimeout, JS timer, setinterval, setTimeout

Blog published in mr. Think: difference between JS timer setTimeout () and setinterval () @ mr. Think

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.