You can put your ajax call in your own function and pass the values of lskey and postData . Thus, localStorage.removeItem(lskey) will refer to the lskey variable in the context of the function, not the loop context.
Example
Declare a function -
function postForm(postdata, lskey) { $.ajax({ type: "POST", async: "false", url: "/Profile/PostForm", data: postdata, success: function(data) { $('#rollinginfo').append('<br>' + data + ',key=' + lskey); localStorage.removeItem(lskey);
Then you can call your function from your loop -
for (var i = 0, len = localStorage.length; i < len; i++) { var lskey = localStorage.key(i); if (lskey.substr(0, 4) === 'form') { var postdata = localStorage.getItem(lskey);
You can also declare a function just before the loop (assigning it to a variable), and then call it in the loop.
var postForm = function(postdata, lskey) { $.ajax({ type: "POST", async: "false", url: "/Profile/PostForm", data: postdata, success: function(data) { $('#rollinginfo').append('<br>' + data + ',key=' + lskey); localStorage.removeItem(lskey);
source share