Example of SetInterval and settimeout usage in JS

Source: Internet
Author: User

Contents of this section: SetInterval and SetTimeout

One, SetInterval () define and use the SetInterval () method to execute a function or expression according to the specified period (in milliseconds). The method keeps looping through the function until the function is explicitly stopped using clearinterval () or the window is closed. The parameter of the Clearinterval () function is the ID value returned by SetInterval (). Syntax SetInterval (code,millisec[, "Lang"]) code required. The function to invoke or the code string to execute. Millisec must. The time interval, in milliseconds, between periodically executing or calling code. Returns a value that can be passed to Window.clearinterval () to suppress periodic execution of code.

Example:

    1. Stop interval Events
Copy Code

Second, the setTimeout () definition and usage the SetTimeout () method is used to call a function or evaluate an expression after a specified number of milliseconds, unlike the SetInterval () method, which is executed only once.

Syntax settimeout (CODE,MILLISEC) code required. The JavaScript code string to execute after the function to invoke. Millisec required. The number of milliseconds to wait before executing the code, in milliseconds. Tip: (1) setTimeout () Although the code executes only once. However, if you want to call multiple times, in addition to using setinterval () You can also let the executed code inside itself again call the SetTimeout () method has reached the goal of multiple executions. (2) Another settimeout () method can also return an ID value to facilitate the cancellation of the use of the SetTimeout () method with the Clearinterval () method.

Example:

Copy Code

For the two methods of SetInterval and settimeout, it is important to note that if you are required to perform an action precisely after every fixed interval, it is best to use setinterval, and if you do not want to cause mutual interference due to successive calls, In particular, each time a function call requires heavy computation and a long processing time, it is best to use settimeout.

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