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
};
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?
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
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.
Then you have the
stop
functionThe spinning happens when the user clicks the button. At that time,
spin
is called(notWheel.spin
)But, only the last
setTimeout
seems to be called.What am I doing wrong?
Here's the full code.