This article details the JavaScript timer and examples. If you need it, you can refer to setting the 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
Use a timer to implement deferred or repeated JavaScript Execution
The window object 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:
The Code is as follows: