I'm currently doing some exercises with JavaScript (Euler problems) and decided to try out an idea for creating a progress bar for problems that use nested loops and take a few seconds. It's just an image of an empty progress bar layered underneath a full one, with a JavaScript function set to change the width of the full bar periodically.
However, this change doesn't seem to happen until after all of the calculations are finished. Does JavaScript put DOM manipulation on hold while it does calculations? If so, is there a simple way around this?
Here's the code I'm running.
JavaScript code:
function updateProgress(calc){
var recorded_calculations = 90357988; //(pre-recorded number)
var percent = (calc / recorded_calculations) * 100;
document.getElementById("full-bar").style.width = percent + "%";
if(percent == 100){
return;
}
setTimeout(function(){
updateProgress(calculations);
}, 1);
}
var calculations = 0;
updateProgress(calculations);
for(lots of increments){
calculations++;
//Logic here
for(lots of increments and){
calculations++;
//Logic here
And CSS code:
#background-bar{
position: absolute;
top: 0px;
left: 0px;
z-index: 1;
width: 100%;
}
#full-bar{
position: absolute;
top: 0px;
left: 0px;
z-index: 2;
width: 0%;
overflow: hidden;
}