Home  >  Article  >  Web Front-end  >  JavaScript timer example analysis_javascript skills

JavaScript timer example analysis_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:15:551064browse

1. What is a JavaScript timer?

In JavaScript, we can execute code after a set time interval instead of executing it immediately after the function is called.

2. Timer type

One-shot timer: Fires only once after the specified delay time.
Interval trigger timer: trigger once every certain time interval

3. Timer method

1): One-time timer

A):setTimeout(): Execute the code after the specified delay time, and execute it once

Syntax: setTimeout(code, delay time);

Parameter description:

1. The function to be called or the code string to be executed.
2. Delay time: the time to wait before executing the code, in milliseconds (1s=1000ms).

B):clearTimeout():Cancel setTimeout() setting

Syntax: clearTimeout(timer)

Parameter description:
timer: ID value returned by setTimeout(). This value identifies the deferred execution code block to be canceled.

Call setTimeout() and clearTimeout() delay methods:

Copy code The code is as follows: