You need to put your loop in the given timeout so that it does not delay the page. Even if your overlay is displayed, no one likes their page freeze
var counter = 0; function rebuild() { showOverlay(); doWork(); } function doWork() { if(counter < 9000){
EDIT . This answer will actually take significantly longer to process the page. Somewhere in the region of 90 seconds, which is quite unacceptable, another alternative could be setting a timeout every 100 iterations, which will add about 1 second to the total load time, but should stop the page freezing.
function doWork() { if(counter < 9000){ // append element if(counter % 100 == 0) { setTimeout(function(){ doWork(); },10); counter++; } else { doWork(); counter++; } } else { hideOverlay(); } }
source share