Javascript中暫停功能的實現代碼_javascript技巧

來源:互聯網
上載者:User
複製代碼 代碼如下:

<script language="javascript">
/*Javascript中暫停功能的實現
Javascript本身沒有暫停功能(sleep不能使用)同時 vbscript也不能使用doEvents,故編寫此函數實現此功能。
javascript作為弱對象語言,一個函數也可以作為一個對象使用。
比如:
function Test(){
 alert("hellow");
 this.NextStep=function(){
  alert("NextStep");
 }
}
我們可以這樣調用 var myTest=new Test();myTest.NextStep();

我們做暫停時候可以吧一個函數分為兩部分,暫停操作前的不變,把要在暫停後執行的代碼放在this.NextStep中。
為了控制暫停和繼續,我們需要編寫兩個函數來分別實現暫停和繼續功能。
暫停函數如下:
*/
function Pause(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 + ")",1000);
}
/*
該函數把要暫停函數放到數組window.eventList裡,同時通過setTimeout來調用繼續函數。

繼續函數如下:
*/

function GoOn(ind){
 var obj=window.eventList[ind];
 window.eventList[ind]=null;
 if (obj.NextStep) obj.NextStep();
 else obj();
}
/*
該函數調用被暫停函數的NextStep方法,如果沒有這個方法則重新調用該函數。


函數編寫完畢,我們可以作如下冊是:
*/
function Test(){
 alert("hellow");
 Pause(this,1000);//調用暫停函數
 this.NextStep=function(){
  alert("NextStep");
 }
}
</script>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.