• I did not completely see thru your code, but mainly, there is a heave function and a much lighter function, each independent of each other and called on individual intervals.

    What is going on here is to understand from the point of how Espruino works, or in other words, how event driven JavaScript environment works.

    First, though, let's look at the code - especially at the on init:

    Every 60 ms, sendData() is invoked,
    Every 10 ms, led() is invoked.

    If you put console.log into your code, you will see that 5 or 6 times led() runs before sendData() is run the first time. Also, when you put console.log at begin and end as first and last in led() and sendData(), you will notice that one always runs completely before it runs again or something else runs (completely).

    To make it bette visible, change the time to a least the 10 fold: 600 and 100 ms, that way Espruino can - most of the time - take care of what it is told to do before a new time event happens and the same ore something else should happen.

    JavaScript is executed as single thread. No things will go in parallel at any time. A time event (or event through a sensor on a pin from outside) create an even/interrupt in the event/interrupt queue in the primary or hardware run cycle. It is called primary because it is closest to the hardware - pin change, timer event - overrun/underrun/0-crossing) - happens and has higher priority then JavaScript execution. When all these high prioritized events/interrupts have been served and respective event with its data/context package is written to the JavaScript event/interrupt queue, the primary cycle goes into idle... but the processor is now free to take care of the events that were put into the JavaScript event queue. The processor 'starts'/resumes the work of interpreting JavaScript: for each entry in the JavaScript is invoked. In your case, led()... and led() again, etc. until sendData() is encountered and worked on. Now it may be that it cannot finish that within 10ms, so led() has and does wait until the processor gets free again for pickup.

    Even though both of your functions called at intervals have nothing to do with each other, they can have an impact on each other. Do you know how much time it take to execute sendData()? Adding getTime() at begin and end with delta calculation will tell you.,

    You may see the effect of the internal JavaScript Event Queue to overflow, even though it is decently sized to handle bursts, but continuous pounding.


Avatar for allObjects @allObjects started