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