亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

js 封裝setInterval問題

js 封裝setInterval問題

大話西游666 2018-12-07 03:24:11
想顯示導入文件進度,js實現了一個導入耗費時間的效果,但是想將其封裝成面向對象的,可是封裝之后沒有正常運行,js 面向對象能力不夠啊,不知道代碼是啥原因,setinterval沒有間隔時間運行,只運行了一次,請高手指導一下原因,謝謝啦! <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>新增屬性值</title> <script type="text/javascript" src="jquery-1.10.2.min.js"></script> </head> <body> <div id="importInfo"></div> <input type="button" onclick="inportProcesser.Start()" value="開始"> <input type="button" onclick="inportProcesser.Close()" value="結束"> <script> function Progresser(intervalms, intervalHandle) { this.intervalMs = intervalms; this. totalIntervalMs = 0; this. timer=null; this.Oninterval = intervalHandle; } Progresser.prototype = { constructor: Progresser, Start: function () { //console.log("Start totalIntervalMs:" + this.totalIntervalMs); this.timer = setInterval( this.IntervalHandle() , this.intervalMs); //console.log("Start timer:" + this.timer); }, Close: function () { //console.log("Close totalIntervalMs:" + this.totalIntervalMs); if (this.timer != null) { //console.log("Close timer:clearInterval pre" + this.timer); clearInterval(this.timer); //console.log("Close timer:clearInterval next" + this.timer); } this.totalIntervalMs = 0; } , IntervalHandle: function () { this.totalIntervalMs += this.intervalMs ; //console.log("IntervalHandle totalIntervalMs:" + this.totalIntervalMs); this.Oninterval(this.totalIntervalMs); //console.log("IntervalHandle timer:" + this.timer); } } var inportProcesser = new Progresser(500, importing2); function importing2(totalIntervalMs) { var second = (totalIntervalMs / 1000.0).toFixed(1); console.log("seconds:" + second + "s.."); } </script> </body> </html>
查看完整描述

3 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

Start: function () {
  //console.log("Start totalIntervalMs:" + this.totalIntervalMs);
  this.timer = setInterval( () => {
    this.IntervalHandle();
  }, this.intervalMs);
  //console.log("Start timer:" + this.timer);
},

這里改成這樣就可以了

查看完整回答
反對 回復 2018-12-24
?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

樓上的解答是正確的,但這是ES6的寫法,其實你的問題是一個“js閉包”的問題而已。

解決辦法可以先聲明一個全局變量

步驟一

var this_ = null;

步驟二

Start: function () {
  //console.log("Start totalIntervalMs:" + this.totalIntervalMs);
  this_ = this;
  this.timer = setInterval( this.IntervalHandle , this.intervalMs);

  /****************************

  說明,問題是你原來是this.IntervalHandle()調用的,但實際上應該是this.IntervalHandle,不需要();

  問題又來了,this.IntervalHandle在setInterval里面單獨調用的話那么this.IntervalHandle函數的

  this指向的就是windos對象了。因為setInterval是全局函數。

  ***********************************************/
  //console.log("Start timer:" + this.timer);
},

步驟三

IntervalHandle: function () {
this_.totalIntervalMs += this_.intervalMs ;
  //console.log("IntervalHandle totalIntervalMs:" + this.totalIntervalMs);
  this_.Oninterval(this_.totalIntervalMs);
  //console.log("IntervalHandle timer:" + this.timer);
}

查看完整回答
反對 回復 2018-12-24
?
holdtom

TA貢獻1805條經驗 獲得超10個贊

謝謝,沒太看明白,對于閉包一直沒理解,js簡單的能寫,深入的話,尤其是封裝的,看著一頭霧水?。?/p>

有時間我還是要好好看看那本js高級程序設計,多研究一下開源代碼!

查看完整回答
反對 回復 2018-12-24
  • 3 回答
  • 0 關注
  • 962 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號