欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

復(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ù)的精確度不那么高的話,使用這個(gè)函數(shù)是個(gè)不錯(cuò)的選擇

下面這個(gè)是復(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)文章

最新評(píng)論