block by kenpenn 92ebaa71696b4c4c3acd672b1bb3f49a

Self-adjusting setTimeout loop

Full Screen

A setTimeout is essentially a polite request to queue code to execute after a specified delay.

The actual delay may vary widely.

This example executes setTimeouts recursively; it compares the ideal time to the actual system time, and adjusts the delay accordingly.

e.g., if the requested delay is 500ms, and the first setTimeout is executed after 510ms, the second setTimeout’s delay is adjusted to 490ms, and so on.

The green dots show when each beat event is fired, the red dots show when each measure event is fired.

Uncorrected loops shows a setTimeout with a fixed delay, the same delay as the adjusted setTimeout target.

adapted from:

index.html

plot.js