setInterval, this, again

前端 未结 5 1525
故里飘歌
故里飘歌 2021-01-27 16:32

I have a problem in regard to setInterval that I can\'t figure out.

There is the problem with the scope when calling setInterval or timeout fro

5条回答
  •  渐次进展
    2021-01-27 17:31

    You should define the value _this in Ship function:

    function Ship(name){
        this.name = name;
        this.ding = function(){
            intervalID1 = setInterval(function(){
                console.log("ding");
            }, 500)      
        }
        var _this = this;
        this.bing = function(){
            intervalID2 = setInterval(function(){
                console.log(_this.name);
            }, 500)      
        }
    }
    

提交回复
热议问题