Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could put your ajax call into its own function and pass the <code>lskey</code> and <code>postData</code> values in. That way <code>localStorage.removeItem(lskey)</code> will refer to the <code>lskey</code> variable in the context of the function rather than the context of the loop.</p> <p><strong>Example</strong></p> <p>Declare the function - </p> <pre><code>function postForm(postdata, lskey) { $.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>Then you can call your function from your loop -</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 postForm(postdata, lskey); } } </code></pre> <p>You could also declare the function just before the loop (assigning it to a variable) and then call it within the loop.</p> <pre><code>var postForm = function(postdata, lskey) { $.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); } }); } 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 postForm(postdata, lskey); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload