代码如下:
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())));
如果大家的程序对sleep()函数的精确度不那么高的话,使用这个函数是个不错的选择
下面这个是复杂些的,需要的朋友也可以参考一下:
 代码如下:
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"); 
 } 
 }