the problem is, whenever I press/release the button, the piezo speaker
beeps until the LEDs are cleared
That's what you program is supposed to do. There is no multitasking in Javascript, just a single thread. As such, the timer to stop the beep expires while your WHILE loop is running, but the code to stop the beep only gets executed when the CPU is idle. That's the case when the function containing the WHILE loop is finished. (There might be better and technical more accurate explanations.)
repeatedly then do this using a function. Start the function with var yolo=setInterval(...) and stop it with clearInterval(yolo). Don't use WHILE or FOR in such cases.
Regarding
pretty simple, right?
No, it's not simple. You need two function while one function with 3 lines would do.
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.
That's what you program is supposed to do. There is no multitasking in Javascript, just a single thread. As such, the timer to stop the beep expires while your WHILE loop is running, but the code to stop the beep only gets executed when the CPU is idle. That's the case when the function containing the WHILE loop is finished. (There might be better and technical more accurate explanations.)
In case you need to write
repeatedly then do this using a function. Start the function with
var yolo=setInterval(...)
and stop it withclearInterval(yolo)
. Don't use WHILE or FOR in such cases.Regarding
No, it's not simple. You need two function while one function with 3 lines would do.