I've experienced the issue that DOM Manipulations only take effect when the browser ran through all iterations, i.e. while, for, -loops.
Example:
var text = document.getElementById("text");
for (var i = 0; i < 100000000; i++) {
if (i % 1000000 == 0) {
setTimeout(function() {
text.innerHTML += "|";
}, 0);
}
}
<p id="text"></p>
I'd like to see a progressbar-ish behavior, instead the DOM is manipulated only when he ran through the for-loop.
I tried an asynchronous approach with setTimeout with no success.
Is there a good way to achieve this?
Since Javascript runs in a single thread and therefore everything is blocked until it is finished processing the calculations.
In order to have a non-blocking calculation, you can use webworkers and update the dom when the worker dispatches the
postMessage
event.Example:
Main code:
WebWorker code:
Further reading: Webworkers