This article mainly introduces the JavaScript timer example analysis. For more information, see
1. what is a JavaScript timer?
In JavaScript, we can execute code after the set interval, rather than immediately after the function is called.
2. timer type
One-time timer: triggered only once after the specified delay time.
Interval trigger timer: triggered at a certain interval.
3. timer method
1): one-time timer
A): setTimeout (): executes the code after the specified delay time.
Syntax: setTimeout (code, latency );
Parameter description:
1. the function to be called or the code string to be executed.
2. latency: The waiting time before code execution, in milliseconds (1 s = 1000 ms ).
B): clearTimeout (): cancels the setTimeout () setting.
Syntax: clearTimeout (timer)
Parameter description:
Timer: the ID value returned by setTimeout. This value identifies the delayed execution code block to be canceled.
Call the setTimeout () and clearTimeout () latency methods:
The code is as follows:
JavaScript timer