I have a long running javascript code that process some kind of render (webgl). It's some kind of "adaptative rendering" in which i calculate a surface more and more precisely over time (meaning that the "resolution" of the scene becomes more and more precise).
I want it to run as long as the user doesn't press a key, or move the mouse. As stated here, i could use setTimeout or setInterval, to give some time to javascript to process other events.
I've tested setTimeout and setInterval delays on this jsFiddle, and i observe the following result:
setTimeout delay: 15-20 ms
setInterval delay: 2-4 ms
It's fast, but still quite hard to use in my situation. I need to be able to react, finish some computations and render in 16 ms to give the user the impression to run at 60 fps.
So here's the question(s):
1) Is there some other (more clever) way to check if there are some incoming user input events to be processed ?
2) Is there something else i can use to make some "long running code interruptible by user" ? (maybe web workers ?)