4

I am running a loop within a click event handler doing the following:

  • run heavy computation (takes 1-3s)
  • call $("<li> + data + </li>").appendTo($("#results"))

The problem is, that the DOM is not refreshed until the loop has terminated. Running the loop for 100 iterations this takes too long, I would like to give a result every time I computed one.

How can I force the DOM to be refreshed?

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143

1 Answers1

4

If you are in a browser that supports it, workers are ideal for this situation.

If not, use setTimeout after each computation to break the callstack and allow the browser to use time on rendering.

Martin Jespersen
  • 25,743
  • 8
  • 56
  • 68