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.
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.
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:
There are other ways to do it, but that one is probably the most readable.