I have a for
loop that cycles through the elements of an Array, constructs a jQuery target, applies a class to it and then removes it.
The class remove
triggers a css3 @keyframes
animation that takes 0.2 seconds. It then proceeds to actually remove the DOM element. This works in theory.
for (var i = playerBuffs.length - 1; i >= 0; i--) {
var target = '#bufflist-' + playerBuffs[i][5];
$(target).addClass('remove');
setTimeout(function(){$(target).remove();},200);
};
There are conditions that have to be true in order for the element to be removed (i didn't paste that code here for simplicity), so sometimes there will be several elements to remove.
However, in that case, the loop runs this code once, sets the timeout, and then instantly reruns the code. So at the point in time where the timeout
triggers, the target
variable already changed. How do i fix this?