Possible Duplicate:
When do items in HTML5 local storage expire?
My web application uses client side storage (sessionStorage
and localStorage
). However, I want this data to be expired after some time (eg. 2 days). It was a super easy task with expiration date of cookies but I don't know how to set an expiration date for HTML5 storage.
Any ideas how to solve this problem? Efficiency and simplicity is important.
LocalStorage has no expiration time, Data in the LocalStorage persist till the user manually delete it.
sessionStorage is similar to localStorage ; the difference is that while data in localStorage doesn't expire, data in sessionStorage is cleared when the page session ends. Whenever a document is loaded in a particular tab in the browser, a unique page session gets created and assigned to that particular tab.
Cookies are intended to be read by the server, whereas localStorage can only be read by the browser. Thus, cookies are restricted to small data volumes, while localStorage can store more data.
For most cases, we use the localStorage object if we want some data to be on the browser. If we want it on the server, then we use cookies, and the sessionStorage is used when we want to destroy the data whenever that specific tab gets closed or the season is closed by the user.
The best you can do is to set a timestamp in storage, and if the user visits your site after a specified amount of time, then you can delete the stored data.
sessionStorage
should work the same way, except when it doesn't even last long enough for your timed expiration. In that case, it'll expire sooner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With