Problem is that the browser engine will hold on to the CSS changes to the page untill the scripts are complete. This is a approach to have a better performance.
Why it happens?
For better understanding look at this example which changes the div
color in each line of the script. Observer the behaviour
$('div').css('background-color','red');
alert('now showing red!');
$('div').css('background-color','yellow');
alert('now showing yellow!');
$('div').css('background-color','green');
alert('now showing green!');
$('div').css('background-color','blue');
alert('now showing blue!');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="width:100px;height:100px;"></div>
You can notice that the background color is not changed until the scripts are completed. This is because the browser engine keeps a track of all the changes happening on the element in its memory and once everything is complete it goes ahead and updates the UI, imagine if the browser had started updating the UI for every call? if there were many changes then you would see performance issue.
How to resolve it?
Look into this example
setTimeout(function(){
$('div').css('background-color','red');
alert('now showing red!');
},0);
setTimeout(function(){
$('div').css('background-color','yellow');
alert('now showing yellow!');
},0);
setTimeout(function(){
$('div').css('background-color','green');
alert('now showing green!');
},0);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="width:100px;height:100px;"></div>
So wrapping your functions inside a setTimeout
will resolve the issue.
That means wrapping your line progressDiv.attr('style', 'width: ' + perc + '%');
within a setTimeout
must fix your issue.
setTimeout(function(){
progressDiv.attr('style', 'width: ' + perc + '%');
},0)
To get more details on why this setTimeout(function(){},0)
works... Here is more detailed explanation
Hope this helps.