I have been making a JavaScript game where it is important that certain variables are saved in order to keep the user’s progress.
I have been using something similar to the function below to save these variables:
function autosave() {
localStorage.setItem("variablename", variablename);
}
setInterval(autosave, 1000);
When the page loads I am using a function that detects whether there is save data, and if there is, it loads all the variables.
The majority of my testing is on iPad’s Safari. The data does save and loads perfectly fine, the only issue is that if a user comes back to the game after a week or so, the saved data is gone. What is the cause of this and how can I make the data ‘last longer’?
I found that
Safari will erase IndexedDB, LocalStorage, Media keys, SessionStorage, and Service Worker registrations after seven days if the user does not interact with the associated website during this period.
I think your code is fine and you cannot make the data ‘last longer’.