I have a loading bar that I'm creating and am passing different milliseconds and style params in var barMovement.
My issue is that in the setTimeout function the last params which has a time of 100 milliseconds is not being output last.
When I console log the results they come out as 100, 200, 200, 100, 200, 300, when it should be 100, 200, 200, 200, 300, 100.
If I understand correctly, the last param of 100 is being output first because it's faster, but is there a fix for this or a way to make setTimeout delay until the previous setTimeout loop is complete?
Thanks!
function BarSequence(style){
this.style = style
this.move = function(styles) {
var i = 0;
for(len = styles.length; i < len; i++){
(function loopy(index) {
setTimeout(function() {
console.log(style[index])
}, i * style[index][0]);
})(i);
}
}
}
var barMovement = new BarSequence([
[100, { width: '10%' }],
[200, { width: '20%' }],
[200, { width: '50%' }],
[200, { width: '80%' }],
[300, { width: '90%' }],
[100, { width: '100%' }]
]);
barMovement.move(barMovement.style);