There are several ways, but they are not perfect
First, loop continuously until the specified time is reached
function sleep(numberMillis) {
var now = new Date();
var exitTime = now.getTime() numberMillis;
while (true) {
now = new Date();
if (now.getTime() > exitTime)
return;
}
}
In fact, the code does not make the script sleep, but instead causes the CPU to quickly ramp up to high load. Most modern browsers will be in suspended animation during this period
Second , use xhr to synchronously request the background program, for example, if 2000 is passed, the background will sleep for 2 seconds and then restart Return, this method also has shortcomings. When N multiple clients request the background, it is very expensive to keep the HTTP connection alive.
In addition, the alert and confirm of the window object can also interrupt the execution of subsequent code.