Usage of SetInterval and setTimeout in JavaScript

Source: Internet
Author: User
When writing H5 games, you often need to regularly refresh pages to achieve the animation effect. Usually, setTimeout () and setInterval () are used. But do you know the usage of SetInterval and setTimeout, the following describes how to use SetInterval and setTimeout in js. For more information, see SetTimeout

Description

SetTimeout (code, millisec)

The setTimeout () method is used to call a function or computing expression after a specified number of milliseconds.

Note: clearTimeout (id_of_settimeout) can be used to terminate a call.


Parameters Description
Code Required. The JavaScript code string to be executed after the function to be called.
Millisec Required. The number of milliseconds to wait before executing the code.

SetTimeinterval

SetInterval (code, millisec [, "lang"])


Parameters Description
Code Required. The function to be called or the code string to be executed.
Millisec Required. The time interval between periodical execution or call codes, in milliseconds.

The setInterval () method can call functions or computation expressions according to the specified period (in milliseconds.

Set latency in JS:

SetInterval is similar to setTimeout. SetTimeout is used to perform an operation after a delay period.

SetTimeout ("function", time) sets a timeout object setInterval ("function", time) sets a timeout object

SetInterval indicates automatic repetition, and setTimeout does not.

ClearTimeout (object) clears the set setTimeout object clearInterval (object) clears the set setInterval object

The setInterval () method can call functions or computation expressions according to the specified period (in milliseconds.

Using a timer to implement JavaScript deferred execution or repeated execution of window objects provides two methods to implement the timer effect, namely window. setTimeout () and window. setInterval. The former allows a piece of code to run after a specified time, while the latter allows a piece of code to run once every time. Their prototype is as follows: window. setTimeout (expression, milliseconds); window. setInterval (expression, milliseconds); expression can be a piece of code enclosed in quotation marks or a function name. at the specified time, the system automatically calls the function, when a function name is used as the call handle, it cannot contain any parameters. When a string is used, you can write the parameters to be passed. The second parameter of the two methods is milliseconds, which indicates the number of milliseconds of delay or repeated execution.

The following describes two methods.

1. window. setTimeout method this method can delay the execution of a function, for example:

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.