I want show and hide a progress bar 10 times. That's why I use a for loop, in which I call the function oneLoop. oneLoop calls the frame function every 100sec. Frame function is used to change the progress bar.
However, the for loop is executed only once. I don't want to use another setInterval function to execute the oneloop function, because unsynchronous events may happen during the intervals and then things become very bad. How can I achieve to execute the oneLoop 10 times and every execution to start after the previous has ended?
The code:
for(var i=0;i<10;i++){
$(".progress").show();
var w = {j:100};
oneLoop(w);
}
function oneLoop(w){
timerId = setInterval(function(){
frame(w)
},100);
}
function frame(w) {
//when the percentage becomes zero, the progress bar closes
if (w.j === 0) {
clearInterval(timerId);
$(".progress").fadeOut();
}
//the percentage is descreased be 1%
else {
w.j = w.j - 1;
$(".progress-bar").width(w.j + '%');
}
}
And the HTML:
<div class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100">
</div>
</div>