Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Updated <a href="http://jsfiddle.net/gvee/Qq247/4/" rel="nofollow">JSFiddle</a></h1> <p>Rather than trying to delay animations and work our timings etc you can simply set a function call to happen when your animation step is complete.</p> <p>The <code>animate()</code> function optionally accepts extra parameters. From <a href="http://api.jquery.com/animate/#animate-properties-duration-easing-complete" rel="nofollow">the manual</a>:</p> <blockquote> <p><strong>.animate( properties [, duration ] [, easing ] [, complete ] )</strong></p> <p>...</p> <blockquote> <p><strong>complete</strong></p> <p>Type: <a href="http://api.jquery.com/Types#Function" rel="nofollow">Function()</a></p> <p>A function to call once the animation is complete.</p> </blockquote> </blockquote> <p>This means you can call a function when your animation is complete. Here's a simple example:</p> <pre><code>$('div').click(function() { $(this).animate({width: 200}, 5000, function() { alert('animation complete'); }); }); </code></pre> <p>In the above code we are popping up a message after the initial animation (width: 200px) is complete.</p> <p>So how is this useful to you? Well, what if we call our second animation once the first has completed, and the 3rd after that had completed, and so on?</p> <pre><code>$('#' + i).animate({width:randomWidth, opacity:'0.3'} , 1000, function() { $(this).animate({height:randomHeight, opacity:'1' }, 1000, function() { $(this).animate({width:'50', opacity:'1'}, 1000, function() { $(this).animate( {height:newHeight, opacity:'1' }, 1000); }); }); }); </code></pre> <p><strong>EDIT:</strong> Here's your code after refactoring:</p> <pre><code>function letsGo(i, newHeight) { var randomWidth = Math.floor(Math.random() * 201 + 50); var randomHeight = Math.floor(Math.random() * 201 + 50); $('#' + i).animate({width:randomWidth, opacity:'0.3'} , 1000, function() { $(this).animate({height:randomHeight, opacity:'1' }, 1000, function() { $(this).animate({width:'50', opacity:'1'}, 1000, function() { $(this).animate( {height:newHeight, opacity:'1' }, 1000); }); }); }); } $('.items').click(function () { var newHeight = 50; var divCount = $('.items').length; for(i=1; i&lt;=divCount; i++) { letsGo(i, newHeight); newHeight += 50; }; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload