it interested me solely as research and personal development. I have a set of functions / variables with names.
within one function I need to call another via setTimeout, but the scope will be 'this'. I'm struggling a bit with this, can't tie it to when setTimeout starts.
var foo = {
ads: ["foo","bar"],
timeDelay: 3,
loadAds: function() {
var al = this.ads.length;
if (!al)
return;
for(var i = 0; i < al; i++) {
setTimeout(function() {
this.scrollAd(this.ads[i]);
}.apply(this), this.timeDelay * 1000);
}
},
scrollAd: function(adBlock) {
console.log(adBlock);
}
};
};
.apply (this) CHANGES the scope since console.log displays the right object back, but it runs the function immediately, and then a warning / warning appears when the callback remains empty:
useless setTimeout call (missing quotes around argument?)
Is there an elegant way to do this at all? i know i can do
var _this = this;
and a link _thisin the announcer callback. for example, in mootools I would use .bind(this)instead ...
, , " " , ...