Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do items in HTML5 local storage expire?

For how long is data stored in localStorage (as part of DOM Storage in HTML5) available? Can I set an expiration time for the data which I put into local storage?

like image 875
user280427 Avatar asked Feb 24 '10 15:02

user280427


People also ask

Do localStorage items expire?

localStorage is similar to sessionStorage , except that while localStorage data has no expiration time, sessionStorage data gets cleared when the page session ends — that is, when the page is closed.

What is expiry time in session storage?

sessionStorage actually doesn't expire when you close the browser, it can span browser sessions.

How does the data is stored in HTML5 web storage?

With web storage, web applications can store data locally within the user's browser. Before HTML5, application data had to be stored in cookies, included in every server request. Web storage is more secure, and large amounts of data can be stored locally, without affecting website performance.

Is local storage permanent?

LocalStorage is not permanent. The storage belongs to the user so the user can clear it if they want to.


1 Answers

I would suggest to store timestamp in the object you store in the localStorage

var object = {value: "value", timestamp: new Date().getTime()} localStorage.setItem("key", JSON.stringify(object)); 

You can parse the object, get the timestamp and compare with the current Date, and if necessary, update the value of the object.

var object = JSON.parse(localStorage.getItem("key")),     dateString = object.timestamp,     now = new Date().getTime().toString();  compareTime(dateString, now); //to implement 

Alternatively, you could use a light-weight wrapper like localstorage-slim.js which handles this for you.

like image 68
sebarmeli Avatar answered Sep 22 '22 19:09

sebarmeli