修改 Meteor.setInterval 函数内的 class 属性
Modifiy class attribute inside Meteor.setInterval function
我需要做的:
TestClass = function(){
this.left =10
this.finterval = function(){
console.log("this.finterval:"+JSON.stringify(intervalId))
this.left = this.left -1
Meteor.clearInterval(intervalId)
}
var intervalId = Meteor.setInterval(this.finterval,1000)
console.log("this.intervalId:" + this.intervalId)
}
有没有办法在区间方法里面修改this.left?
this
总是引用当前作用域,因此如果您嵌套函数,则需要将其分配给另一个 var(例如 self
):
TestClass = function(){
this.left =10
var self = this;
this.finterval = function(){
console.log("this.finterval:"+JSON.stringify(intervalId))
self.left = self.left -1
Meteor.clearInterval(intervalId)
}
var intervalId = Meteor.setInterval(this.finterval,1000)
console.log("this.intervalId:" + this.intervalId)
}
我需要做的:
TestClass = function(){
this.left =10
this.finterval = function(){
console.log("this.finterval:"+JSON.stringify(intervalId))
this.left = this.left -1
Meteor.clearInterval(intervalId)
}
var intervalId = Meteor.setInterval(this.finterval,1000)
console.log("this.intervalId:" + this.intervalId)
}
有没有办法在区间方法里面修改this.left?
this
总是引用当前作用域,因此如果您嵌套函数,则需要将其分配给另一个 var(例如 self
):
TestClass = function(){
this.left =10
var self = this;
this.finterval = function(){
console.log("this.finterval:"+JSON.stringify(intervalId))
self.left = self.left -1
Meteor.clearInterval(intervalId)
}
var intervalId = Meteor.setInterval(this.finterval,1000)
console.log("this.intervalId:" + this.intervalId)
}