My JavaScript method works fine in Chrome, taking about 2000 ms to iterate over ~200 inputs, insert values, and trigger onchange events. However, it's a different story in IE where it takes about 10000 ms.
To show the progress of this process, I decided to implement a progress bar but encountered an issue - the browser wouldn't repaint the progress bar until the very end when it immediately jumps to 100%.
I tried three different methods I found during my research:
- Running the progress bar update in a setTimeout with zero or non-zero timeout
- Accessing the height property of the element or adjacent elements to force repaint
- Show/hide the element and access the offsetHeight property simultaneously
Here is a snippet of my code:
<div class="cg-progress-bar">
<div class="cg-progress-bar-completed">
</div>
<div class="cg-inline-block cg-progress-bar-force-repaint">
</div>
</div>
JavaScript:
var progressBarCurrent = 0;
var progressBarTotal = 236;
$.each(sqlData, function(column, value){
//performing some tasks to update input values
//update progress bar
progressBarCurrent++;
if (progressBarCurrent % 22 === 0) {
var percentageComplete = Math.floor( (progressBarCurrent/progressBarTotal)*100 ) + "%";
var bar = $(".cg-progress-bar-completed").width(percentageComplete)[0];
//workarounds to force repaint
bar.style.display = 'none';
bar.offsetHeight;
bar.style.display = '';
setTimeout(function() { bar.style.display = 'block'}, 0);
$(".cg-progress-bar-completed").animate({ width: percentageComplete }, 100).height();
bar.appendChild(document.createTextNode(' '));
$(bar).hide().show(0);
bar.appendChild(document.createTextNode(' '));
$(window).trigger("resize");
}
}
});
How can I effectively force the browser to repaint?