javascript - "this" within setTimeout self executing function is undefined -
i know within settimeout correspond default window, have been using bind , passing through self executing function, when settimeout runs (it's fine on first run, called teabot._managetearound(), it's not fine when self executing) undefined. here's code, going wrong? (i have deleted lines of code might not relevant). :)
teabot.prototype._managetearound = function(originalmessage, channel){ var self = this; self.teamaker = this._getuserbyid(originalmessage.user); //now wait 3 minutes people send order self._runtimer(self,channel); } teabot.prototype._runtimer =function(self, channel) { // stuff console.log(self.teamaker.name); //undefined var interval = self.interval, teamaker = self.teamaker; console.log("self.interval " + self.interval); if(interval === 0){ interval++; self.interval = interval; settimeout(self._runtimer.bind(self, channel), 180000); }else{ self.interval = 0; } }
this line problematic:
settimeout(self._runtimer.bind(self, channel), 180000);
function teabot.prototype._runtimer
expects self
first param - function.prototype.bind()
first param context
(function's this
). try use this:
settimeout(self._runtimer.bind(self, self, channel), 180000);
or leave context
empty, becouse not using this
@ all:
settimeout(self._runtimer.bind(undefined, self, channel), 180000);
Comments
Post a Comment