@SergeP, var h; and later used in: h = setInterval(...); returns a handle that you can use for clearInterval(h), because this handle - number - keeps moving... (yes it is 1 when it is the very first setInterval/setTimeout... but you do not know what other parts of the software are using setInterval/setTimeout, so your next one is something totally different.
It is good practice to check for multiples of same setInterval, because that can really crate issues... You can prevent that by checking against that handle you get for that particular if (!h) { h = setInterval(...); }: But to be clear: the variable where you store the handle is not cleared when the interval/timeout is cleared or the timeout happens. Therefore, a good practice is to have the handle cleared like this h = clearInterval(h);respectiveh = clearTimeout(h);, and in the timeout function as first thingh = undefined;`, if it matters.
The other thing I could think of is that some numeric / precision thing (number of significant / stored digits) and the way numbers are stored can play games with you... especially when it comes to fractions of time. Very defensive, tight and robust programming are the only way to detect that and the way out. I'm not paranoid, but somethings the absence of 'x' does not mean the presensec of '!x'... ;-)
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.
@SergeP,
var h;
and later used in:h = setInterval(...);
returns a handle that you can use forclearInterval(h)
, because this handle - number - keeps moving... (yes it is 1 when it is the very first setInterval/setTimeout... but you do not know what other parts of the software are using setInterval/setTimeout, so your next one is something totally different.It is good practice to check for multiples of same setInterval, because that can really crate issues... You can prevent that by checking against that handle you get for that particular
if (!h) { h = setInterval(...); }
: But to be clear: the variable where you store the handle is not cleared when the interval/timeout is cleared or the timeout happens. Therefore, a good practice is to have the handle cleared like thish = clearInterval(h);
respective
h = clearTimeout(h);
, and in the timeout function as first thing
h = undefined;
`, if it matters.The other thing I could think of is that some numeric / precision thing (number of significant / stored digits) and the way numbers are stored can play games with you... especially when it comes to fractions of time. Very defensive, tight and robust programming are the only way to detect that and the way out. I'm not paranoid, but somethings the absence of 'x' does not mean the presensec of '!x'... ;-)