title: closure inside a for loop - callback with loop variable as parameter
body: <p>I am using jQuery "GET" in a loop to obtain several results from the server.  I want to include the loop index as a fixed parameter to the call back but its not working.</p>
  
  <p>(I followed the advice of <a href="https://stackoverflow.com/questions/939032/jquery-pass-more-parameters-into-callback">this article</a> on how to do it.)</p>
  
  <p>However, the value I get in the call back is completely not what I expect – rather than each loop index value, it is always equal to the exit value of the index.</p>
  
  <p>ie. the code fragment here prints out '16' for each execution of the callback.  How do I get it to print 1, 2, 3...  (I realize the order might be different, that's fine)</p>
  
  <p>In addition to the code below, I've tried several ways to specify the call back function, eg. <code>function(data, textStatus) { return test(data, textStatus, idx); }, 'text');</code> etc.</p>
  
  <p>How is this supposed to work?</p>
  
  <pre><code>function test(data, textStatus, siteNo)
  {
      console.log("siteNo=" + siteNo);
  }
  
  function loadConfigLists()
  {
      var siteReport;
      // retrieve site configuration
      jQuery.get("svGetSiteConfig.php", function(data, textStatus) 
      {
          // retrieve port configuration for all sites
          for (var idx=1; idx&lt;=15; idx++)
          {
              var probeIP = siteConfigArray[idx].siteIP;
              if (probeIP != "" &amp;&amp; probeIP != null)
              jQuery.get("svGetPortInfo.php?svSiteIpAddr=" + probeIP+"&amp;s="+idx, 
                      function(data, textStatus) { test(data, textStatus, idx); }, 'text'); 
              else // IP value is blank
                  siteConfigArray[idx].portManifest = null;
          }
          }
      }, 'text'); 
  }
  </code></pre>