Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok, here is one approach to achieve this:</p> <p>Within your httpRequests success handler, attach a "timestamp" to your response:</p> <pre class="lang-js prettyprint-override"><code>// Assuming you are working with JSON data var response = JSON.parse(this.responseText); response.timestamp = new Date(); // For the purpose of this example, we persist our response to properties Ti.App.Properties.setObject('cachedResponse', response); </code></pre> <p>In your button eventListener we will check for the time that has passed</p> <pre class="lang-js prettyprint-override"><code>button.addEventListener('click', function() { var cachedResponse = Ti.App.Properties.getObject('cachedResponse', { timestamp: false }); if(cachedResponse.timestamp) { if(getHoursDiff(cachedResponse.timestamp, new Date()) &gt; 24) { // Last request older than 24 hours, reload data } else { // Last request was within 24 hours, use cached data } } else { // No data has been saved yet, load Data } }); </code></pre> <p>This function calculates the time difference in hours</p> <pre class="lang-js prettyprint-override"><code>// http://blogs.digitss.com/javascript/calculate-datetime-difference-simple-javascript-code-snippet/ function getHoursDiff(earlierDate, laterDate) { var nTotalDiff = laterDate.getTime() - earlierDate.getTime(); var hours = Math.floor(nTotalDiff/1000/60/60); return hours; } </code></pre> <p><br><strong>Note</strong><br> This is not a complete example, you have to optimize and change the code to your needs.</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