Note that there are some explanatory texts on larger screens.

plurals
  1. POCallback functions and jQuery's each iterator
    primarykey
    data
    text
    <p>I'm having difficulty calling a callback function after an <em>each</em> iterator.</p> <p>Here's an example:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="jquery-1.4.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="move.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div&gt; &lt;div class="ani" style="position:relative; display: inline-block; top:10px; left:0px; width:30px; height:30px; background:#ddd;"&gt;&lt;/div&gt; &lt;div class="ani" style="position:relative; display: inline-block; top:10px; left:110px; width:30px; height:30px; background:#ddd;"&gt;&lt;/div&gt; &lt;div class="ani" style="position:relative; display: inline-block; top:10px; left:210px; width:30px; height:30px; background:#ddd;"&gt;&lt;/div&gt; &lt;div class="ani" style="position:relative; display: inline-block; top:10px; left:310px; width:30px; height:30px; background:#ddd;"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; $(document).ready(function(){ $ani = $('div.ani'); var redFlash = function(){ $ani.eq(0).css('background-color','#e35'); }; var goingDown = function(){ $ani.each(function(i){ $(this).animate({'top':'100px'}, (i+1)*2000); }),redFlash()}; goingDown(); }); </code></pre> <p>The <em>redFlash</em> function doesn't get called though. I've also tried:</p> <pre><code>var goingDown = function(){ $ani.each(function(i){ $(this).animate({'top':'100px'}, (i+1)*2000); }),function(){ $ani.eq(0).css('background-color','#e35'); }; }; </code></pre> <p>to no avail.</p> <p>How can I make redFlash run <strong>after</strong> all the animation inside the <em>each</em> iterator has completed? Is there a way I can make <em>goingDown</em> callback to <em>redFlash</em> after the animation completes?</p> <p>Also, does anyone know of any good online resources or books for javascript/jquery callback functions? The book I have is a little flaky on this topic.</p> <p><strong>Edit: Solved using Pointy's pointer on using counters.</strong></p> <pre><code>$(document).ready(function(){ $ani = $('div.ani'); var redFlash = function(){ $ani.eq(0).css('background-color','#e35'); }; var ani_size = $ani.length-1; console.debug(ani_size); var goingDown = function(){ $ani.each(function(i){ $(this).animate({'top':'100px'}, (i+1)*2000, function(){ console.debug(i); if (i == ani_size){ redFlash(); } }); }); }; goingDown(); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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