You are reading a single comment by @joppiesaus and its replies. Click here to read the full conversation.
  • So I'm still enjoying to work on my fruitmachine, but the way I implement multiple timeouts don't seem to work correctly.
    Basically, I've 3 LED's, or fruits - or wheels, and I stop them one by one in order of time. When I spin the wheel, I tell it when to stop spinning. It works by just setting the velocity of the wheel to 0.

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

    Then you have the stop function

    Wheel.prototype.stop = function()
    {
      this.vel = 0.0;
    };
    

    The spinning happens when the user clicks the button. At that time, spin is called(not Wheel.spin)

    function spin()
    {
      for (var i = 0; i < N_LEDS;) // loop trough all wheels, spin them
      {
        wheels[i].spin((++i * 500));
      }
      
      setTimeout(function(){
         //... other code. It checks some win conditions and lights and stuff after every wheel stopped.
      }, (N_LEDS + 2.5) * 500);
    

    But, only the last setTimeout seems to be called.
    What am I doing wrong?

    Here's the full code.

About

Avatar for joppiesaus @joppiesaus started