In a previous post, timing inconsistencies using requestAnimationFrame
were suggested to be solved with an infinite requestAnimationFrame
loop e.g. as follows.
function workaroundLoop() {
requestAnimationFrame(workaroundLoop);
};
I actually tested this with an oscilloscope, and this looping does nicely improve timing precision in all browsers (not only Chrome, as suggested in that post).
The new problem however is that it introduces a 1-frame delay in changing the display (when a command for that is given). For example, if the line document.getElementById('stimulus_id').textContent = "NEW DISPLAY";
is executed, the text "NEW DISPLAY" will not be displayed in the upcoming repaint of the screen, but only in the following one. So, if there is no looping, the painting is always immediate and follows the command within 0-16 ms (given the 60Hz refresh rate), but if the loop is going on, the painting always gets delayed and only happens 16-32 ms following the command.
Remarkably, if I use canvas with desynchronized: true
for the display, there is no delay in Chrome, Microsoft Edge, Opera, but there is still a delay in Firefox. However, apart from that this does not work in Firefox, I would also not know how to use this solution for non-canvas display (such as simple text).
Any ideas about (a) why exactly this is happening and (b) how to solve this? That is: I still want to benefit of precise timing, but I also want to be able to display (paint) elements immediately.