I have a massive script to run synchronously, and I want to show the message "Processing, wait ..." before it starts, so the user does not wonder why the page froze for a few seconds. I am trying to do something like:
messageBox.html("Processing, please wait..."); // run hefty script messageBox.html("Finished!");
But page blocks before the message are displayed, even if the instruction comes messageBox.html()first. Why is this?
messageBox.html()
Sometimes it makes sense to run a "hefty script" in timeout.
messageBox.html("Processing, please wait..."); setTimeout(function () { heftyScript(); messageBox.html("Finished!"); }, 1);
, , , ( "" script). - , script ( ).
- : http://www.html5rocks.com/en/tutorials/workers/basics/
UPDATE:
- , . .
:
function heftyScript() { var arr = [...]; var chunk_start = 0; function do_chunk() { for( var i = 0; i < 100; ++i ) { // 100 items per chunk if( chunk_start >= arr.length) return; process( arr[chunk_start++] ); // process one element } window.setTimeout(do_chunk,10); } do_chunk(); }
It depends on where the time is spent. If it loads and parses the JavaScript file, it messageBox.html()must be pure html, or you do it in a script block before referencing an external file. If the time spent launches this long function, then it setTimeout(function () { heftyScript(); messageBox.html('finished'); }, 1);works wonderfully.
setTimeout(function () { heftyScript(); messageBox.html('finished'); }, 1);