2013-05-17 90 views
0

因爲我需要傳遞一個匿名函數到setInterval如果我想要參數,我嘗試使用下面的代碼。本來我打電話給this.countUp,但由於返回NaN我做了一些閱讀,發現SO上的.call(this)解決方案。但是,當我將它與匿名函數(我承認自己有點霧)相結合時,我現在在Firebug中獲得了TypeError: this.countUp is undefinedJavaScript中的類變量和setInterval

我想我不需要使count可訪問,也不需要playBeep方法,但讓我們假裝我想讓我能夠理解我在做這個代碼時做了什麼錯誤。

function workout() { 
     var beep = new Audio("beep1.wav"); 
     this.timerWorkout; //three timers in object scope so I can clear later from a different method 
     this.timerCounter; 
     this.timerCoolDown; 
     this.count = 0; 

     this.startWorkout = function() { 
      alert(this.count); 
      this.timerWorkout = setTimeout(this.playBeep, 30 * 1000); //workout beep - 30 seconds 
      this.timerCounter = setInterval(function() {this.countUp.call(this)}, 1000); //on screen timer - every second 

     } 

     this.startCoolDown = function() { 
      this.timerCoolDown = setTimeout(this.playBeep, 10 * 1000); //cooldown beep - 10 seconds 
     } 

     this.playBeep = function() { 
      beep.play(); //plays beep WAV 
     } 

     this.countUp = function() { 
      this.count++; 
      document.getElementById("counter").innerHTML = this.count; 
     } 

    } 

    var workout1 = new workout() 

回答

4

startWorkout使用bind(this)

this.timerCounter = setInterval(function() {this.countUp()}.bind(this), 1000); 
+0

完美的作品。謝謝你的時間。 – armadadrive

+0

:)閱讀'bind()',你會使用它很多。 – vivek

1

會發生什麼事是的setInterval正在改變你爲它調用的函數裏面的this值。您需要將this存儲在單獨的變量中以防止它被覆蓋。

function workout() { 
    var self = this; 
    // ... 

    this.startWorkout = function() { 
      alert(this.count); 
      this.timerWorkout = setTimeout(self.playBeep, 30 * 1000); // this method works 
      this.timerCounter = setInterval(function() {self.countUp}, 1000); // so does this one 
    } 
} 
+0

啊,OK。它適用於'playBeep'方法,因爲沒有參數。我也將嘗試上面發佈的'bind'解決方案。 – armadadrive

0

js中變量作用域限制函數的原因。因此,當您嘗試在嵌套函數中使用this時,您會獲得指向另一個對象的鏈接。創建變量var that = this;到更高級別的函數中,然後在任何嵌套函數中使用它來引用您正確的上下文。