make localStorage or sessionStorage expire like cookies

html, javascript, local-storage

Solution

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.

Problem

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.

Original source

Related problems