object - Class variables in JavaScript and setInterval - Stack Overflow

Since I need to pass an anonymous function to setInterval if I want parameters, I tried using the below

Since I need to pass an anonymous function to setInterval if I want parameters, I tried using the below code. Originally I had it calling this.countUp, but as that returned NaN I did some reading and found the .call(this) solution on SO. However, when I bined that with the anonymous function (which I admit I'm a bit foggy on), I'm now getting TypeError: this.countUp is undefined in Firebug.

I suppose I don't need to make count accessible, nor the playBeep method, but let's pretend I wanted to so that I can understand what I'm doing wrong with this code.

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

Since I need to pass an anonymous function to setInterval if I want parameters, I tried using the below code. Originally I had it calling this.countUp, but as that returned NaN I did some reading and found the .call(this) solution on SO. However, when I bined that with the anonymous function (which I admit I'm a bit foggy on), I'm now getting TypeError: this.countUp is undefined in Firebug.

I suppose I don't need to make count accessible, nor the playBeep method, but let's pretend I wanted to so that I can understand what I'm doing wrong with this code.

    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()
Share Improve this question asked May 17, 2013 at 13:51 armadadrivearmadadrive 9813 gold badges14 silver badges44 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Inside startWorkout use bind(this) :

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

What happens is setInterval is changing the value of this inside the function you provide for it to call. You need to store this in a separate variable to prevent it from getting overridden.

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
     }
}

The reason that the variable scope in js is limited on function. So when you are trying to use this inside a nested function, you get a link to another object. Create a variable var that = this; into a higher-level function, and then use it in any nested function that would refer you to the correct context.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745351762a4623875.html

相关推荐

  • object - Class variables in JavaScript and setInterval - Stack Overflow

    Since I need to pass an anonymous function to setInterval if I want parameters, I tried using the below

    7小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信