Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use Ajax to save a cache file?
    primarykey
    data
    text
    <p>I have a javascript/ajax function which looks for a json file hosted on another server. I need my function to perform the following actions:</p> <ol> <li>Get json file from external server</li> <li>Save json file on its own local server</li> <li>Check to see if json file is older than 1 hr</li> <li>If it isnt older than 1hr then use that data</li> <li>If it is older than 1hr - re-download from external server and overwrite local version</li> </ol> <p>Currently my function gets the data from the external sever everytime it is called. I need to add in this caching functionality but am unsure how to takle it.</p> <p>Can anyone offer any advise?</p> <p>Here my code:</p> <pre><code> function ajaxLoad(page,url,type,variety,category , widgetClickedId) { /* * Checking the clicked id is the same as passed in one * TODO refactor so the clicked id only ever gets passed */ if(widgetId != widgetClickedId &amp;&amp; widgetClickedId != undefined) { return false; } var website = $('#button-print'+widgetId).data("website"); var page = $('#button-print'+widgetId).data("page"); $.ajax({ type: 'GET', url: 'www.myothersite.com/api/v1/productchoice.json?website='+website, async: true, jsonp: 'callback', dataType: 'jsonp', success: function(productchoice) { if(productchoice['brand'+widgetId] == 'null' || productchoice['brand'+widgetId] == undefined) { productchoice['brand'+widgetId] = ''; } //check that all values are not null , if not , then show the widget if( productchoice['brand'+widgetId] == '' &amp;&amp; productchoice['subject'+widgetId] == '' &amp;&amp; productchoice['market'+widgetId] == '' &amp;&amp; productchoice['type'+widgetId] == '' &amp;&amp; productchoice['bookazinebrands'+widgetId] == '') { //is this corect? $('#gdmContainer'+widgetId).hide(); //return false; } else { $('#gdmContainer'+widgetId).show(); } setRibbons(productchoice.ribbonTop , productchoice.ribbonBottom); getProductChoice( productchoice ); setLoveTitle( productchoice); setDefaultBtn(productchoice['defaultBtn'+widgetId]); return productchoice; } }); </code></pre> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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