This article mainly shares with you the analysis of setInterval and setTimeout examples in JS, hoping to help everyone.
1 setInterval
is a function. Function: You can repeatedly execute a certain code block (function)
setInterval(function, time interval), how long to execute the first parameter (function block) repeatedly
Note: Time unit millisecond 1000 = 1 second
Function names cannot be written in parentheses. This way of writing is wrong: fn()
2 clearInterval clear timer (setInterval)
注意: 在开启一个新的定时器之前,先清除定时器
3 chrome new tab page plug-in
Infinity new tab page
4 aLi is a class array. When setting the style in a loop, be careful to use a for loop instead of for in
5 The syntax structure of setTimeout is similar to setInterval, but the meaning is different.
setTimeout waits for how long to execute a certain code block, similar to timing*
setTimeout(fn, 3000), wait until 3 seconds later to execute the fn function Timing*
setInterval(fn, 3000), execute the fn function every 3 seconds Engine
6 Recursive function
A function can call other functions or itself. This kind of function is called a recursive function
Note: Recursive function has two meanings: progression and regression
Application scenarios of recursive functions: A large problem can be split into multiple similar sub-problems, and the solutions to these sub-problems are similar,
At this time, you can use the recursive function,
For example: consider this requirement: printing 1-3 in the body as a big problem
Sub-problem: Play a number each time, solution, document.write()
Recursively create folders, recursively delete folders
Note: When writing recursive functions, pay attention to the exit. Set exit conditions for recursive calls
7 Clear timer
setInterval Execute a piece of code (loop) every certain time Clear: clearInterval
setTimeout Wait until a certain amount of time to execute a piece of code ( Execute once) clear: clearTimeout
1 setInterval
Is a function, function: can repeatedly execute a certain code block (function)
setInterval(function, time interval), how long to execute the first parameter (function block) repeatedly
Note: Time unit millisecond 1000 = 1 second
Function names cannot be written in parentheses. This way of writing is wrong: fn()
2 clearInterval clear timer (setInterval)
注意: 在开启一个新的定时器之前,先清除定时器
3 chrome new tab page plug-in
Infinity new tab page
4 aLi is a class array. When setting the style in a loop, be careful to use a for loop instead of for in
5 The syntax structure of setTimeout is similar to setInterval, but the meaning is different.
setTimeout waits for how long to execute a certain code block, similar to timing*
setTimeout(fn, 3000), wait until 3 seconds later to execute the fn function Timing*
setInterval(fn, 3000), execute the fn function every 3 seconds Engine
6 Recursive function
A function can call other functions or itself. This kind of function is called a recursive function
Note: Recursive function has two meanings: progression and regression
Application scenarios of recursive functions: A large problem can be split into multiple similar sub-problems, and the solutions to these sub-problems are similar,
At this time, you can use the recursive function,
For example: consider this requirement: printing 1-3 in the body as a big problem
Sub-problem: Play a number each time, solution, document.write()
Recursively create folders, recursively delete folders
Note: When writing recursive functions, pay attention to the exit. Set exit conditions for recursive calls
7 Clear timer
setInterval Execute a piece of code (loop) every certain time Clear: clearInterval
setTimeout Wait until a certain amount of time to execute a piece of code ( Execute once) Clear: clearTimeout
Related recommendations:
Detailed explanation of the use of setInterval in JavaScript
Detailed explanation of setTimeout() and setInterval() in JavaScript timer
The above is the detailed content of Analysis of setInterval and setTimeout examples in JS. For more information, please follow other related articles on the PHP Chinese website!