In a real sense, setTimeout and setInterval do not really run the code at the specified time, but put the code into the execution queue at the specified time.

setTimeout:

Executes code only once, within a specified time.

setInterval:

Periodically queues code to wait for execution.


When writing code, we occasionally use setInterval to execute certain code, but when the code blocks, setInterval also periodically queues the code to execute, and so on. Can cause serious problems such as code blocking.

When recursive setTimeOut is used, such a situation can be completely avoided. When the code is blocked, the following code will not be executed, which can be a good way to avoid such situation.

The code is as follows: