How is it possible to use this inside of setInterval and setTimeout calls?
I want to use it like :
function myObj() {
this.func = function(args) {
setTimeout(function() {
this.func(args);
}, 1000);
}
}
Some time ago I did it for .onclick event this way :
this.click = function(func) {
this.elem.onclick = (function(func, obj) {return function(){func.apply(obj)} })(func,this);
};
but I don’t know how I can do it for intervals and timeouts.
The easiest way is to just save
thisinto a local. The localselfisn’t changed by the context in whichsetIntervalandsetTimeoutcallbacks are invoked. It will instead maintain the originalthisvalue