title: Jquery ajax in loop
body: <p>I am writing a Sql convertor which handles mysql to mongodb. I am writing an interface to my converter with ajax.</p>
  
  <p>Ajax is handling huge convertions. This put limit to mysql select code and make convertions parted by 1000 at a time.</p>
  
  <p>my code is : </p>
  
  <pre><code>$(document).ready(function()
  {
  &lt;!--Start JQuery Ajax Requests--&gt;
  var pages;
  var type;
  $("input").click(function(event) {
      pages = $(this).attr("icr");
      type = $(this).attr("id");
      runRequest(0);
  });
  
  function runRequest(num){
  
      if (num &gt; 3){
      $("#console").append("Finish!!!");
      return;
      }
  
      $.ajax
      ({
      type: "POST",
      url: "#",
      async: false,
      data: "type="+type+"&amp;page="+num*1000,
      success: function(msg){
          $("#console").ajaxComplete(function(event, request, settings)
          {
              $("#console").append(msg);
              runRequest(num+1);
          });
      }
      });
  }
  });
  </code></pre>
  
  <p>this code have to run 3 times of the same function and send the limit number by multiplying it. But somehow runRequest's num variable never reaches the 3, firebug console shows num as 1 or sometimes 2 and it repeats infinite. How to avoid it and make it run only 3 syncronised calls?</p>