Home >Web Front-end >JS Tutorial >JavaScript timer example analysis_javascript skills

JavaScript timer example analysis_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 16:15:551200browse

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:




          
                                                                                                          
          
                                                                                           

         

Copy code The code is as follows:




          
                                                                                                          
          
                                                                                           

         

A):setInterval(): During execution, execute the code at specified intervals after loading the page
Syntax: setInterval(code, interaction time);

Parameter description:

1. Code: The function to be called or the code string to be executed.

2. Interaction time: The time interval between periodic execution or calling of expressions, measured in milliseconds (1s=1000ms).

Return value:

A value that can be passed to clearInterval() to cancel periodic execution of the "code".

Call function format (assuming there is a clock() function):

setInterval("clock()",1000) or setInterval(clock,1000)

B): The clearInterval() method can cancel the interaction time set by setInterval()

Syntax: clearInterval(timer)

Parameter description:

timer: ID value returned by setInterval().

Call setInterval() and clearInterval() to execute the interval execution method instance

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn