I\'m trying to make a div fade in/out that\'s within an each statement. The problem is that next item is called before the fade in/out is complete.
How about this, animate by going through each items in the array within the function?
var elements = [ "one", "two", "three"];
animate(elements);
function animate( elements, index )
{
if(!index) index = 0;
var box = '#' + elements[index];
var $$box = $("#box");
console.log( 'start - ' + elements[index] );
$$box.fadeOut( 500, function( )
{
console.log('showing - ' + elements[index]);
$$box.fadeIn( 500, function() {
console.log( 'end - ' + elements[index] );
if(elements[++index]) animate(elements, index);
} ).css('backgroundColor','white');
});
}
You can even loop back to the start if you want:
var elements = [ "one", "two", "three"];
animate(elements);
function animate( elements, index )
{
if(!index) index = 0;
var box = '#' + elements[index];
var $$box = $(box);
console.log( 'start - ' + elements[index] );
$$box.fadeOut( 500, function( )
{
console.log('showing - ' + elements[index]);
$$box.fadeIn( 500, function() {
$$box.css('backgroundColor','white');
console.log( 'end - ' + elements[index] );
// go to next element, or first element if at end
index = ++index % (elements.length);
animate(elements, index);
} );
}).css('backgroundColor', 'aqua');
}