0

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.

5
  • Several problems here. One of them is that object1 is not the same as _this.object1. Commented Aug 20, 2013 at 17:38
  • So how could I reference object1 from within a setTimeout? Commented Aug 20, 2013 at 17:40
  • It looks like object1 and _this are being set globally, not on this (instanceof roulette) and not as variables within new roulette. Commented Aug 20, 2013 at 17:47
  • Sorry, this is my mistake, following the original code object1 would've been set as an instance of roulette, I have edited the OP accordingly. Commented Aug 20, 2013 at 17:49
  • Well, you fixed it now. But _this = this should still be var _this = this, as @PaulS. pointed out. Commented Aug 20, 2013 at 17:53

2 Answers 2

2

Re-factored your code a little, made it consistent with it's use of this vs _this inside functions. Removed () from functions passed into setTimeout.

All seems to work as you intended.

function Roulette() {
    var _this = this;
    this.object1 = {
        x: 0
    };
    this.spin = function spin() {
        if (undefined !== this.shuffleTimer) clearTimeout(this.shuffleStart);
        this.shuffleStart = setTimeout(this.timeoutFunction, this.object1.x);
    };
    this.timeoutFunction = function timeoutFunction() {
        _this.object1.x += 5;
        console.log(_this.object1.x);
        if(_this.object1.x < 5000){
          _this.shuffleStart = setTimeout(timeoutFunction, _this.object1.x);
        }
    };
}
var r = new Roulette();
r.spin();
Sign up to request clarification or add additional context in comments.

2 Comments

I suggest you use _this everywhere inside spin; it's being used as an event handler, according to the OP.
When I do newRoulette = new Roulette() The event handler simply calls newRoulette.spin(). Thank you this answer works perfectly!
0

setTimeout wants to have an inline function as its callback. Change this:

 this.shuffleStart = setTimeout(_this.timeoutFunction(), _this.object1.x);

To this:

 var ctx = this;
 this.shuffleStart = setTimeout(function(){ctx.timeoutFunction()}, _this.object1.x);

5 Comments

That's not it unfortunately, same result.
@styke are you making the change in both of the two places you call setTimeout?
@styke How are you calling the whole thing? Also, do you have your browser console open to check for errors?
A self invoking function creates a new instance of the roulette object, after which the spin function is called on mouseclick.
@styke The onclick handler introduces an additional problem... I suggest you edit the question, and add a jsfiddle example that reproduces all those aspects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.