It seems that you are faced with the fact that Javascript is single-threaded, but there are no browsers.
Basically, you cannot run multiple Javascript at the same time. However, the browser still needs to do something other than Javascript execution and continues to do so whenever possible. The problem is that the DOM modification is synchronous (i.e., JS execution stops before it is completed) or asynchronously (JS execution continues) is not defined. Most browsers do the latter. But JS execution still has a rather high priority, and many DOM updates are delayed until JS execution stops and stops. A warning is a great example because it is waiting for user input.
The way to do what you want to do is to use setTimeout() , which allows the current JS fragment to finish, then the browser can complete the DOM update, and then it can execute JS waiting to be executed by timeout.
source share