My brain its going to explode.. why this dont work? im trying to animate a few divs with time interval and trying to write less code but this dont work
var cargaCont = function(){
o = 1;
var animacion = function(i){
$('#page2txt'+i).animate({
height:'20'
},200,function(){
$('#page2img'+i).animate({
left:'0',
right:'0'
},200,function(){
i++;
return i;
});
});
}
while(o < 3){
setTimeout(function(){o = animacion(o);},200);
}
}
The problem with this code:
is by the time the functions delayed by
setTimeoutare executed,ois already 3 and therefore all calls toanimacionpass 3 instead of 1 and 2.To circumvent this problem, you should “localize” the value of
oby using an immediate function.This makes the
oused by the functions insetTimeoutto be bound to theoof the local function and not theoof thecargaContfunction.