setTimeout is often used to delay the execution of a certain function. The usage is:
Sometimes setTimeout(function...,0) is used for asynchronous processing; for example:
Function f returns before the function processor set by setTimeout;
Be especially careful when using asynchronous processing, especially when using closure features;
For example:
For students who are using this method for the first time, they may think that the program will print 0...9, but it actually prints 10 10s;
The problem is that when the loop completes, the function is executed and i has become 10, which is 10 used in console.log(i)!
If your purpose is to print 0...9, then you can change the way and use function parameters to save 0...9 (in fact, closures are also used):