I've made a prototype class of Bot. My issue is, after I've created it, I call it's init(). It correctly returns this value "a 5000" in an alert. However when that prototype function calls getUpdates() it no longer reaching the this value and giving "b undefined". I've even tried this.self = this; in the constructor but no luck.
After struggling, I've found adding () on the self.getUpdates call in the setInterval made it get the value properly then a another problem, setInterval only loops once. I've tried making a setTimeout and making it call itself inside getUpdates but got "too much recursion script.js:30:1". I've sometimes got "uncaught exception: out of memory "
I was originally using "var privateVars <-> this.methods" without much issue but switched to "this.publicVars <-> Class.prototype.methods" since I've read they supposed to be faster and less memory but this prototype method giving me issues. I've tried browsing Google but no luck. I would prefer to have timer start on init().
Here is my code:
var Bot = function () {
"use strict";
this.updateInterval = 5000;
this.updateTimer = null;
};
Bot.prototype.getUpdates = function () {
"use strict";
var self = this;
alert("b " + self.updateInterval); // returns "b undefined"
};
Bot.prototype.init = function () {
"use strict";
var self = this;
$.get(/* pretend url is here*/, function (data, status) {
alert("a " + self.updateInterval); // returns "a 5000"
self.updateTimer = setInterval(self.getUpdates, self.updateInterval);
});
};
window.bot = new Bot();
window.bot.init();
Any help or advice would be appreciated. But I'm thinking prototype isn't the way to go if it includes timers.