The console. The log (' execution after 1 second)
}, 1000)
Const time1=Date. Now ();
For (the let I=0; i <100000; I++) {
The console. The log (I)
}
Const time2=Date. Now ();
The console. The log (time2 - time1)
As shown above, the for loop to perform in my local needs 11887 milliseconds, and set the timer 1 s after the execution, but according to the event loop mechanism, the content of the print in the timer can be carried in a for loop after print it out, do not conform to the requirements of the preset 1 s, how to modify can waiting for 1 s can also be performed in a for loop?
CodePudding user response:
Js is single-threaded, anything this test doesn't mean youActual business processing, we can through the asynchronous method to avoid phenomenon caused by single thread waiting for jam
CodePudding user response:
The present code is waiting for 1 s performed for loop but wants to timer output still need to wait for loop was overCodePudding user response:
Through asynchronous can be solvedCodePudding user response:
A webworker to handle complex calculation procedures, setTimeout can be written in the main thread, so as to ensure setTimeout performed after 1 s