However, these two functions are asynchronous, and the code behind them will continue to execute during the timing process. Then write a sleep() function yourself. There are also some implementation methods circulating on the Internet, but I found the following method to be easy to understand and practical, so I share it with you here:
console.log('start...');
console.log('now time: ' Date(/d{10,10}/.exec(Date.now())));
function sleep(sleepTime) {
for(var start = Date.now(); Date.now() - start <= sleepTime; ) { }
}
sleep(5000); // sleep 5 seconds
console.log('end.. .');
console.log('end time: ' Date(/d{10,10}/.exec(Date.now())));
If your program is not very accurate with the sleep() function, using this function is a good choice
The following is more complicated, friends who need it can also refer to it:
function Sleep(obj,iMinSecond)
{
if (window.eventList==null)
window.eventList=new Array();
var ind=-1;
for (var i=0;i {
if (window.eventList[i]==null)
{
window.eventList[ i]=obj;
ind=i;
break;
}
}
if (ind==-1)
{
ind=window.eventList.length ;
window.eventList[ind]=obj;
}
setTimeout("GoOn(" ind ")",iMinSecond);
}
function GoOn(ind)
{
var obj=window.eventList[ind];
window.eventList[ind]=null;
if (obj.NextStep) obj.NextStep();
else obj();
}
function Test()
{
alert("sleep");
Sleep(this,100);
this.NextStep=function()
{
alert("continue");
}
}