title: jQuery Ajax call in loop losing local variable reference
body: <p>I am making several jQuery ajax calls within a loop.  Each time one of the ajax calls return I need to reference a value corresponding to the original ajax call.  My current code doesn't work properly, in that the value of the <code>lskey</code> variable has been altered by further loop iterations.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>for (var i = 0, len = localStorage.length; i &lt; len; i++) {
          var lskey = localStorage.key(i);
          if (lskey.substr(0, 4) === 'form') {
              var postdata = localStorage.getItem(lskey); // Get the form data
              $.ajax({
                  type: "POST",
                  async: "false",
                  url: "/Profile/PostForm",
                  data: postdata,
                  success: function (data) {
                      $('#rollinginfo').append('&lt;br&gt;' + data + ',key=' + lskey);
                      localStorage.removeItem(lskey); // Remove the relevant localStorage entry
                  }
              , error: function (data) { $('#rollinginfo').append('&lt;br /&gt;ERR:' + data); }
              });
  
  
          }
      } 
  </code></pre>
  
  <p>The problem is that <code>lskey</code> is being altered each time the loop executes, and therefore the success callback does not have a reference to the value of lskey that existed at the time of the call.</p>
  
  <p>How do I reference the correct value of <code>lskey</code> for each success callback?</p>