tags:

views:

580

answers:

3

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.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script>

<div id='one'>one</div>
<div id='two'>two</div>
<div id='three'>three</div>

<script>
$.each([ "one", "two", "three"], function() {
  console.log( 'start - ' + this );
  animate( this );
  console.log( 'end - ' + this );
});

function animate( id )
{
  box = '#' + id;

  $(box).fadeOut( 500, function( )
  {

    console.log('showing - ' + id);
    $(box).fadeIn( 500 );
    $(box).css('backgroundColor','white');

  });

}
</script>

console shows -

start - one
end - one
start - two
end - two
start - three
end - three
showing - one
showing - two
showing - three

I would want something like -

start - one
showing - one
end - one
start - two
showing - two
end - two
start - three
showing - three
end - three

So how can I wait for each 'each' to be completely finished before moving on to the next value?

+1  A: 

Sounds like you are trying to "cycle" through a list of divs. Have you checked out the jQuery Cycle plugin?

PetersenDidIt
well, this is just a simple example. I might want to do ajax calls with the values in the array, call other functions that do other things, or something else. I made the following to show a simple example of how to get each item to wait for the previous one to be completely over.
scott
+7  A: 

Your going to have to use callbacks - functions that get executed when the current function is finished. To do this with .fadeOut you would do:

$('#element').fadeOut( 400, myFunction );

myFunction would not be called until fadeOut was completed. AJAX calls with $.get also can have callback functions.

Here's an example that works, although I'm sure there's a better way:

function animate(myArray, start_index) {

    // Stealing this line from Sam, who posted below.
    if(!start_index) start_index = 0;

    next_index = start_index+1;
    if(next_index > myArray.length) { return; }

    box = '#' + myArray[start_index]; 
    $(box).fadeOut(500, function() { animate(myArray,next_index); });
}

and then in your document.ready you'd call:

animate(theArray);
Erik
+1  A: 

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');
}
Sam
This is a more complete answer to the exact example posted then mine; although in my defense I was trying to keep it a simple example of recursion, like my comment here: http://stackoverflow.com/questions/2168485/wait-for-each-jquery
Erik
Yours is a simpler example, and sometimes that can be beneficial (easier to understand, depends on what you want to do)
Sam