I tried to access ‘this’ after the implement of setinterval function, but couldn’t access this from the function. Illustrated as below:
apple:'apple',
orange:'orange',
pie:'pie',
initialize:function(){
var self = this;
setInterval(this.print(),100);
},
print:function(){
console.log('print '+ this.apple + ' - ' + this.orange + ' - ' + this.pie);
}
Output: undefined
If I pass in the this as parameter for the function, the interval only called once and stop.
apple:'apple',
orange:'orange',
pie:'pie',
initialize:function(){
var self = this;
setInterval(this.print(this),100);
},
print:function(self){
console.log('print '+ self.apple + ' - ' + self.orange + ' - ' + self.pie);
}
Output: print apple – orange – pie (stopped after that)
How would I able to access the ‘this’ variables, after I called setinterval?
Here’s the jsfiddle example.
http://jsfiddle.net/bwB9W/21/