Say I have an object:
var myObj = function {
this.count = 0;
}
myObj.prototype {
setCount : function() {
var interval = setInterval(function() {
this.count++;
}, 500);
}
}
The issue is count is always undefined within the setInterval so i can never increment the count variable within myObj to be something other than 0.
thischanges depending on the function being called (and how it said function is called).The anonymous function you pass to
setIntervalis a different function.Copy the value to a non-magic variable first.
See the
thisproblem in the documentation.