简体   繁体   中英

Local storage - saving data from multiple tabs

I'm using a localStorage to store some stuff ( profile visits history ).

Every time profile is open, i get object, add more entries and update object. This can lead to data loss (example: tab1:open, tab2:open, tab1:save, tab2:save).

Now, if I hold cntrl and open many new tabs at once, how to pervent data loss? Didn't find anyhing about localStorage locking.

You can increment a variable instead of getting a whole new object, or push that new object into an array and calculate the total views getting the length of that array. The localStorage is shared across the different tabs as long as they are in the same domain.

Option 1.

Check and put a variable (eg editing ) to localStorage when a tab is opened. So if another tab is open, you can create a warning on new tab. Clear that variable onPageUnload event. ( Pessimistic Lock )

Option 2.

Update/put a variable (eg saveTime or saveCount ) on save action. When a tab is opening, retrieve this variable and on save action check if the variable is remaning the same. If value is not the same, warn the user that he/she is editing an oldest version of the data . ( Optimistic Lock )

Optimistic Lock vs Pessimistic Lock

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM