-
• #2
Well, one guess is that (like in a browser)
setTimeout();
doesn't set thethis
variable to what you'd expect, so whenstop
is called fromsetTimeout(this.stop, stopt);
and it tries to dothis.vel = 0
, it's actually settingvel
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.
-
• #3
Just to add that this is pretty much standard JS. For example:
function Foo(x) { this.baz = x; } Foo.prototype.bar = function() { return this.baz; }; var f = new Foo("Hello World"); console.log(f.bar()); // prints "Hello World" function printer(fn) { console.log(fn()); } printer(f.bar); // prints 'undefined'
-
• #4
Thanks a lot! I often forget those kinds of things. Now it works!
-
• #5
@Gordon, didn't you recently add .bind()? Would that now be even a better option - resource-wise - and for sure from a point of avoiding globals...?
I guess the code would then look like this:
Wheel.prototype.spin = function(stopt) // stopt = stoptime { this.vel = (2 + Math.random() * 5) / 40; // give the wheel a velocity setTimeout(this.stop.bind(this), stopt); // tell it when to stop };
Adding this .bind() was a great move! Still have to get used to it having started JS with 1.3...
-
• #6
Yes, that works too - and as you say it's more efficient.
It's just that it's not very readable if you don't know what
bind
does :)
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.