0

I'm working on the offline mode of a web app and I have an issue with the local storage. I've been through my code again and again and it can't find what's wrong. May you help me ?

Brief context : The user can send requests (forms) and it is saved in our database. Then, later, he/she can consult every requests saved in the database.

Principle : When the user sends his/her request, the code checks if there is an internet connection. If it detects one, an Ajax request is sent. If it doesn't detect one, the data of the request (form) is saved into the local storage. Every 30 seconds, the code checks the connection again and if it detects one, it retrieves the saved data, sends an Ajax request, and deletes the item from the local store.

Issue : The item is never deleted from the local store, so it keeps sending the Ajax request ...

Code pieces :

Checking the local store

var run = function(){
      // checking connection
      Offline.check();  
      // if connection ok
      if(Offline.state === 'up'){
           // if there is data saved   
           if(localStorage.length !== 0){   
              // sending every pending requests back
              for (var i = 0, len = localStorage.length; i < len; i++){
                 var request = localStorage.getItem(localStorage.key(i));
                 var jsonR = JSON.parse(request);
                 jQuery.ajax({
                       type: "POST",
                       url: "ajaxAMS_request.php",  
                       data: jsonR,
                       dataType: "html",
                       async: true, 
                       success: function(){     
                           // deleting item from data store
                           localStorage.removeItem(localStorage.key(i));
                       } 
                  });
               }   
            }
        }
    };
setInterval(run, 30000);  // 30 sec interval

If no connection, saving data

var dataRequest = {type: 'add', id_place: idPlace, id_user : idUser}; // JSON
var strJSONDataR = JSON.stringify(dataRequest); // turned into string for storing
localStorage.setItem(savingKey, strJSONDataR); // stored
Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
LucDrt
  • 57
  • 4
  • 1
    It may seem surprising, but yes, the answers to that question really answer this one. The issue is that `i` in the ajax callback is not what you think it is. **Note:** Um, not sure what happened, but I meant to use this question, not that one: http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example That one probably works to, but this is the one I meant. – T.J. Crowder Jan 08 '16 at 14:51
  • @T.J.Crowder or rather, it isn't anymore what you think it is :D It will be len – Icepickle Jan 08 '16 at 14:52
  • Sorry, but I really don't get it ... – LucDrt Jan 08 '16 at 15:00
  • Ok, I've understood with that other link. Thank you very much, I've fixed my code and it's working :) – LucDrt Jan 08 '16 at 15:13

0 Answers0