I've got some code that follows the following structure:
function = roulette(){
_this = this;
this.spin = spin;
this.timeoutFunction = timeoutFunction;
this.object1 = {
x : 0
}
function spin(){
if (typeof this.shuffleTimer !== 'undefined') {
clearTimeout(_this.shuffleStart);
}
this.shuffleStart = setTimeout(_this.timeoutFunction(), _this.object1.x);
}
function timeoutFunction(){
this.object1.x += 5;
//do some DOM manipulation here
console.log(_this.object.x);
if(this.object1.x < 5000){
this.shuffleStart = setTimeout(_this.timeoutFunction(), _this.object1.x);
}
}
}
It is definitely not working as intended - while the console.log logs that this.object1.x is increasing, it does so way too fast and at a uniform rate, which is not how it would've been working if the setTimeout in timeoutFunction was set off after an increased amount of time each time it was called.
object1is not the same as_this.object1.object1and_thisare being set globally, not onthis(instanceof roulette) and not as variables withinnew roulette._this = thisshould still bevar _this = this, as @PaulS. pointed out.