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: