Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>UPDATE 2:</h1> <p>Here is a working example using $.Deferred:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function () { $.Deferred(function (defer) { var i; var duration = 1000; var pipe = function(defer, index, duration) { return defer.pipe(function () { return $(".elem[order='" + index + "']").animate({opacity: 0}, duration); }); }; defer.resolve(); for (i = 0; i &lt; 5; i++) { defer = pipe(defer, i, duration); } defer.done(function () { console.log("Done, removing all .elem"); $(".elem").remove(); }); }); }); &lt;/script&gt; &lt;style type="text/css"&gt; body { padding: 20px; } .elem { border: 1px solid #000; padding: 10px; opacity: 1.0; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="elem" order="0"&gt;elem 0&lt;/div&gt; &lt;div class="elem" order="1"&gt;elem 1&lt;/div&gt; &lt;div class="elem" order="2"&gt;elem 2&lt;/div&gt; &lt;div class="elem" order="3"&gt;elem 3&lt;/div&gt; &lt;div class="elem" order="4"&gt;elem 4&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <hr> <h2>UPDATE 1, BASED ON NEWLY INTRODUCED DELAY</h2> <p>To answer your comment below, I think your best bet is to simply fire off the callback on the animation that fires last. Since you're in full control of deciding duration (i.e. it's not a randomly generated duration), then you could compute what the last duration will be and use that to decide if it will be the correct iteration in which to fire the callback:</p> <pre><code>var i, callback, delay; var onComplete = function () { console.log("Animations complete!"); }; for (i = 0; i &lt; somevalue; i++) { delay = i * duration; callback = delay === ((somevalue - 1) * duration) ? onComplete : $.noop; //callback = i === somevalue - 1 ? onComplete : $.noop; //this can work too but doesn't check duration $(".elem[order='" + i + "']").delay(delay).animate({opacity:0}, duration, callback); } </code></pre> <hr> <h3>ORIGINAL ANSWER</h3> <p>It looks like you want all of these elements to start animating at the same time, and have the same duration. Based on this, I would suggest doing a single animate call, and modifying your approach to gathering the collection of elements.</p> <p>Example:</p> <pre><code>var i, elements, selector; for (i = 0; i &lt; somevalue; i++) { selector = ".elem[order='" + i + "']"; if (i === 0) { // Start the collection up with the first element elements = $(selector); } else { // Append element to the collection elements.add(selector); } } // Fire up the animation on all elements elements.animate({opacity:0}, duration, function () { console.log("Animation complete!"); }); </code></pre> <p><em>I haven't actually ran this code, but I'm pretty confident it should work.</em></p>
    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