Answer the question
In order to leave comments, you need to log in
How to properly organize saving in localStorage?
Goal: Save the user's data entered in the form for half an hour until another user is selected.
As I see:
I do serialize forms, write to localStorage.
I save the user ID.
I am writing a function to clear localStorage:
function clearLocalStorage() {
let limit = 3600000 * 24;
let localStorageInitTime = localStorage.getItem('localStorageInitTime');
if (localStorageInitTime === null) {
localStorage.setItem('localStorageInitTime', +new Date());
} else if(+new Date() - localStorageInitTime > limit)
localStorage.clear();
localStorage.setItem('localStorageInitTime', +new Date());
}
Answer the question
In order to leave comments, you need to log in
How to associate user ID with form data?
const users = [
{
uID: 'xxxxxxx1',
data: 'serialized form data',
initTime: 4617390192123
},
{
uID: 'xxxxxxx2',
data: 'serialized form data',
initTime: 1353451451323
}
]
localStorage.setItem('users', JSON.stringify(users))
const findUserByID = ID => users.find(e => e.uID === ID)
You are storing data incorrectly. Ideally, the data should be presented as an object with all the comprehensive information (field data, date of saving, and so on), overtake this object into JSON and only then save it to localStorage. Sense to produce a bunch of keys?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question