I have a little script that runs a simulation, of which I want to render the results 'live':
for ( i < simulation steps ) {
do_simulation();
render_with_flot();
}
I noticed that the plot only gets rendered after the last step.
- Is there a way to 'suspend' javascript somehow to allow the rendering to run after each iteration?
- Or is there a way to make flot run synchronously?
- Or do I need to set my own timeouts for each iteration of the for-loop? This seems like kind of a hassle.
Depends how fast it needs to run, but the best way would be to use
SetInterval
Pseudocode / hand-written-javascript-that-probably-doesnt-run:
As an alternative to @PhonicUK's solution, you could do a setTimeout() at the end of work() to schedule the next call to work(), giving the rendering a chance to happen & not tying yourself to any particular refresh rate.