I have a student who wants the LEDs on the circuit playground to be green when the board is moving and purple when the board is still.
She is using the accelerometer’s ‘change’ event to trigger the code that makes the LEDs green.
I was not sure how to know if the board was not in motion using the accelerometer, so I suggested using setTimeout() to make the lights purple after 1000 ms.
This did not work. Each time the board moves, the event triggers many times and we end up with multiple timers going off and making the LEDs purple while the ‘change’ event turns them back to green.
So, I suggested using clearTimeout() to stop the existing timeout (using clearTimeout()) before calling setTimeout().
The only problem is that the FIRST time the event triggers, there is no timer.
Is it a problem to call clearTimeout(0) the first time around? 0 is the initial value we are using for the ‘time’ variable. The code seems to work. I am just wondering if anyone knows how clearTimeout() handles a parameter value that does not match an existing setTimeout() value.
This is what we are using and it works.
