Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>All you'll need to do is create a javascript object with the data that's returned from the Ajax request. As part of that object, store the date of when the object was cached. You can then compare that date with the current time each time you call the function.</p> <p>Here's how I might tackle this:</p> <pre><code> // Variable to contain cached object. Make sure it name it better... var x = null; 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"); // Creating a new date and adding an hour to it. var d = new Date(); d.setHours(d.getHours() + 1); // Test to ensure x is not null and it's not older than an hour old if (x != null &amp;&amp; Date.parse(x.date) &lt; d) { // Do your stuff with the cached file here } else { $.ajax({ // set up your ajax request ... success: function (productchoice) { // Make sure to cache the object x.file = productchoice; x.date = new Date(); // Do your stuff with the cached file here } }); } } </code></pre> <p>It would be beneficial to put your normal "success" operations in a separate function to avoid repeating the code. If you do this you'd use the cached variable (<code>x</code> in the example code) instead of using the <code>productchoice</code> variable (i.e. <code>x.file['brand' + widgetId]</code>).</p>
 

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