One is that things like setTimeout schedule some work to happen and then return immediately - so your for loop will be scheduling 4 timers at 16*400, 17*400, 1*400 and 9*400ms from the point of execution (rather than after each other) - so rando[2] will execute before all the others because it's 1*400ms (I'm not sure that's quite what you want?).
The other one is that because all those Timeouts are executing after the loop has finished, h is set to the value it'll be at the end of the loop (which is 4) for all of the timeouts.
For that issue, you can call setTimeout from a function:
function doTimer(my_h) {
return setTimeout(function() {
console.log(rando[my_h]);
}, rando[my_h]*400)
}
for (h=0;h<4;h++) {
console.log(h);
g[h] = doTimer(h);
}
Since there's a new function call for each timeout, h will be unique for each function to will dump the right value.
You can also pass data in to setTimeout, so for instance you could do:
For what you're want to do with the blinking, it might be worth trying something a bit different. You could have a single function that you call that actually alters the array it works from.
So for instance the code below runs blinker - which looks at the first item in the array - if it's nonzero it blinks the led, decrements it, and calls itself again after a short delay. If it's zero it takes it off the array, waits for a longer period, then calls again.
Finally if the array is empty it stops - there's no more work to do...
var rando = [16, 17, 1, 9, 8, 7, 15, 18, 19, 1];
function blinker() {
if (!rando.length) return; // nothing to do
if (rando[0]>0) {
rando[0]--;
digitalPulse(LED1,1,100);
setTimeout(blinker, 200);
} else { // first element is 0 - make a gap between sets of pulses
rando.shift(); // take the first item off
setTimeout(blinker, 1000); // wait
}
}
blinker();
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.
Ahh - I think you're hitting a few issues here...
One is that things like
setTimeout
schedule some work to happen and then return immediately - so yourfor
loop will be scheduling 4 timers at 16*400, 17*400, 1*400 and 9*400ms from the point of execution (rather than after each other) - so rando[2] will execute before all the others because it's 1*400ms (I'm not sure that's quite what you want?).The other one is that because all those Timeouts are executing after the loop has finished,
h
is set to the value it'll be at the end of the loop (which is4
) for all of the timeouts.For that issue, you can call setTimeout from a function:
Since there's a new function call for each timeout,
h
will be unique for each function to will dump the right value.You can also pass data in to setTimeout, so for instance you could do:
Both should work.
For what you're want to do with the blinking, it might be worth trying something a bit different. You could have a single function that you call that actually alters the array it works from.
So for instance the code below runs
blinker
- which looks at the first item in the array - if it's nonzero it blinks the led, decrements it, and calls itself again after a short delay. If it's zero it takes it off the array, waits for a longer period, then calls again.Finally if the array is empty it stops - there's no more work to do...