I would like invoke a method of an js object within the very same object method via setTimeout:
var ads = {
init: function() {
ads.display_ads();
},
display_ads: function() {
console.log('Displaying Ads');
setTimeout('ads.display_ads()', 5000);
}
}
However, I’m getting this error message:
ads is not defined
setTimeout('ads.display_ads()', 2000);
What am I missing here? How would i alter the string within the setTimeout function?
Thanks for your help!
Edit: I use firefox on mac.
Just change it to
ads.display_ads, note that this is not aString. i.e.As @FelixKling points out in his comment below, be careful about what
thisrefers to inads.display_ads. Ifads.display_adsis called viaads.init()orads.display_ads()thiswill be theadsObject. However, if called viasetTimeoutthiswill bewindow.If the context is important though, you can pass an anonymous function to
setTimeout, which in turn callsads.display_ads():or