You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Well, one guess is that (like in a browser) setTimeout(); doesn't set the this variable to what you'd expect, so when stop is called from setTimeout(this.stop, stopt); and it tries to do this.vel = 0, it's actually setting vel on the global variable, not on your wheel.

    To fix it, try:

    Wheel.prototype.spin = function(stopt) // stopt = stoptime
    {
      this.vel = (2 + Math.random() * 5) / 40; // give the wheel a velocity
      var wheel = this;
      setTimeout(function() {
        wheel.stop();
      }, stopt); // tell it when to stop
    };
    

    There are other ways to do it, but that one is probably the most readable.

About

Avatar for Gordon @Gordon started