title: JQuery - When calling a function multiple times in a row, why only the callback of the last call is executed properly?
body: <ol>
  <li>There is a function that triggers a callback when finished;</li>
  <li>I call this function 2 or 3 times consecutively;</li>
  <li>Only the callback of the last call does what was expected;</li>
  </ol>
  
  <p>The following description and code exemplifies the true scenario.</p>
  
  <p>I have three pairs of divs. I need to toggle one div of each pair and change the state of the remaining visible divs when it's pair is no more visible.</p>
  
  <p>Then, I made a function to hide a div and change the background color of another one. I did this because I'd like to call this function whenever the user clicked a button to show a description and other non-essential items.</p>
  
  <p>Unfortunately, the result isn't the one I expected. If the user cause the function to be called more than one time, without letting the function finish it's task, only the last bound callback will behave properly, the other ones won't change the background color of the div or will be out of sync with the other div due to delay.</p>
  
  <p>Here is the javascript:</p>
  
  <pre><code>function toggleLayer(layerId,layerId2) {
      //element1 is the first div of the pair. The one to be hidden.
      element1 = $("#"+layerId);
      //element2 is the second div of the pair. The background-color of this one will be changed when the element1 is hidden.
      element2 = $("#"+layerId2);
  
      //Hiding the first div
      element1.toggle("slow",function() {
          //Changing the color of the second div
          element2.toggleClass("blue");
      });
  }
  </code></pre>
  
  <p>Here is the full HTML, just copy and paste to test:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Test&lt;/title&gt;
  &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  &lt;!--
  
      function toggleLayer(layerId,layerId2) {
          //element1 is the first div of the pair. The one to be hidden.
          element1 = $("#"+layerId);
          //element2 is the second div of the pair. The background-color of this one will be changed when the element1 is hidden.
          element2 = $("#"+layerId2);
  
          //Hiding the first div
          element1.toggle("slow",function() {
              //Changing the color of the second div
              element2.toggleClass("blue");
          });
      }
  
  --&gt;
  &lt;/script&gt;
  &lt;style type="text/css"&gt;
      div{
          width:100px;height:300px;background-color:red;float:left;
      }
      .blue {
          background-color: blue !important;
      }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;div id="d1"&gt;&lt;/div&gt;
  &lt;div id="d11"&gt;&lt;/div&gt;
  &lt;div id="d2"&gt;&lt;/div&gt;
  &lt;div id="d22"&gt;&lt;/div&gt;
  &lt;div id="d3"&gt;&lt;/div&gt;
  &lt;div id="d33"&gt;&lt;/div&gt;
  
  
  
  &lt;input type="button" onclick="toggleLayer('d1','d11');" value="1" style="clear:both;float:left;" /&gt;
  &lt;input type="button" onclick="toggleLayer('d2','d22');" value="2" style="clear:both;float:left;" /&gt;
  &lt;input type="button" onclick="toggleLayer('d3','d33');" value="3" style="clear:both;float:left;" /&gt;
  &lt;input type="button" onclick="toggleLayer('d1','d11');toggleLayer('d2','d22');" value="1+2" style="clear:both;float:left;" /&gt;
  &lt;input type="button" onclick="toggleLayer('d1','d11');toggleLayer('d2','d22');toggleLayer('d3','d33');" value="1+2+3" style="clear:both;float:left;" /&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I was expecting the callback to finish it's task before trying to execute the next call, but it seems the browser is trying to execute them all at the same time. Why does it happen and how do I do it properly?</p>