M
M
McThinker2020-01-17 11:46:52
JavaScript
McThinker, 2020-01-17 11:46:52

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());
}

How to associate user ID with form data?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nvdfxx, 2020-01-17
@McThinker

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)

N
Nadim Zakirov, 2020-01-17
@zkrvndm

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 question

Ask a Question

731 491 924 answers to any question