Sandy.Arv
Sandy.Arv

Reputation: 605

Storing data during page refresh

Let me try to put this as simple as I can. I have a piece of code which parses a dynamic website and retrieves JSON object. The website updates its data every half an hour. So i have programmed my code in a way, it refreshes every half hour using

<meta http-equiv="refresh" content="1800" />
<script>

      function requestCrossDomain(site, callback) {
      if (!site) {
          alert('No site was passed.');
          return false;
      }
      var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=?';
      $.getJSON(yql, cbFunc);
      function cbFunc(data) {
          if (data.results[0]) {
              data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
                  window[callback](data);
          } else throw new Error('Nothing returned from getJSON.');
      }
  }

      var url = 'https://www.emcsg.com/marketdata/priceinformation';

      requestCrossDomain(url, 'someFunction');



function someFunction(results){
var html = $(results);




        var table_pre = html.find(".view72PeriodsWrapper").find(".previous");
        var table_cur = html.find(".view72PeriodsWrapper").find(".current");
        var table_fut = html.find(".view72PeriodsWrapper").find(".future");
        var previous_tab = []
        for ( var i = 0; i < table_pre.length; i++ ) {
            var previous = ($(table_pre[i])).html();
            previous_tab.push(previous.split('<td>').join('').split('</td>'));

        }
        var current_tab = []
        for ( var i = 0; i < table_cur.length; i++ ) {
            var current = ($(table_cur[i])).html();
            current_tab.push(current.split('<td>').join('').split('</td>'));

        }
        var future_tab = []
        for ( var i = 0; i < table_fut.length; i++ ) {
            var future = ($(table_fut[i])).html();
            future_tab.push(future.split('<td>').join('').split('</td>'));

        }
        var jsonObject = JSON.stringify(previous_tab) ;
        var jsonObject2 = JSON.stringify(current_tab);
        var jsonObject3 = JSON.stringify(future_tab);


        jsonObject = jsonObject.concat(jsonObject2, jsonObject3);

        $('#json').text(jsonObject);
      }     

</script>

Now since my program is going to be running for like say, forever, every time the program refreshes, the data is overwritten. I want to know a way to store the previous data and append it in an array for every consecutive refreshes.

I tried doing it through local storage, but since my entire code is set to re run every half hour, the local storage also gets overwritten during refreshes.

Now, How do i store my data?

EDIT: Have uploaded my entire code

Upvotes: 0

Views: 58

Answers (1)

Jack Guy
Jack Guy

Reputation: 8523

Local storage should work fine for you, provided you aren't overwriting the older data.

var data = [];
if (window.localStorage['my-data'] !== undefined) {
  data = JSON.parse(window.localStorage['my-data']);
}
data.push(myNewData);
window.localStorage['my-data'] = JSON.stringify(data);

Upvotes: 2

Related Questions