Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery deferred ajax cache
    primarykey
    data
    text
    <p>I read the top answer to <a href="https://stackoverflow.com/questions/4869609/how-can-jquery-deferred-be-used">this question</a> regarding the use of <a href="http://api.jquery.com/category/deferred-object/" rel="nofollow noreferrer">jQuery Deferred</a>. </p> <p>I'm looping through an array of IDs. For each ID, I need to get data pertaining to it either from an ajax request, or from a cache if an ajax request had already successfully returned the data before.</p> <p>During each loop, I use a $.when() to observe whether getData() returns something from cache or a successful ajax call, before processing that ID. The current problem is that the ID processing proceeds anyways without waiting for getData()'s ajax to succeed. </p> <p>Some pseudocode: </p> <pre><code>var IDs = ["1", "2", "1", "3", "1"]; //ID "1" is repeated //data for "1" should should require ajax get the first time //subsequent processing should get data for "1" from dataCache var dataCache = []; function getData(ID){ if (/*data for ID in dataCache*/){ //return data pertaining to ID from dataCache } else { return $.getJSON("returnJSONDataByID/" + ID, function(resp){ //push resp data to dataCache }) } } for (/*each item i in IDs*/){ $.when(getData(IDs[i])).then(function(){ //process IDs[i] data //this is the resolved handler, which should be executed //when either getData() returns data from the dataCache, //or $.getJSON succeeds //PROBLEM: this is currently executing every loop and //and doesn't wait for the ajax to return resp }) } </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.
 

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