亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript實現(xiàn)Sleep函數(shù)的代碼

 更新時間:2007年03月04日 00:00:00   作者:  
大家知道,JavaScript中沒有內(nèi)置我們常用的sleep()函數(shù),只有定時器setTimeout()和循環(huán)定時器setInterval()

但是,這兩個函數(shù)是異步的,在計時的過程中它們后面的代碼還是會繼續(xù)執(zhí)行。那就自己來寫個sleep()函數(shù)吧,網(wǎng)上也流傳了一些實現(xiàn)方法,不過我發(fā)現(xiàn)下面這個方法簡單易懂而且實用,所以在這里分享給大家:

復(fù)制代碼 代碼如下:

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())));

如果大家的程序?qū)leep()函數(shù)的精確度不那么高的話,使用這個函數(shù)是個不錯的選擇

下面這個是復(fù)雜些的,需要的朋友也可以參考一下:

復(fù)制代碼 代碼如下:

function Sleep(obj,iMinSecond)
 { 
  if (window.eventList==null) 
  window.eventList=new Array(); 
  var ind=-1;
  for (var i=0;i<window.eventList.length;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");
  }
 }

相關(guān)文章

最新評論