html5 local storage如何设置过期时间?

HTML5的local storage被设计成永久地保存数据在本地,对于敏感数据这不是好事,那么有没有办法给local storage存储的数据设置过期时间,到点之后自动删除?

另外,有人说可以手动删除,如何手动删除,难道是在浏览器里“清除浏览数据”清除?

删除localStorage有两种方法。一种是removeItem(key)删除指定的值,第二种是localStorage.clear()删除全部的值。可以选择性触发。
也可以结合setTimeout来进行定时触发。

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
建议你存储timestamp到你localStorage的那个对象中
你可以解析这个对象,得到timestamp,并且和当前DATE比较,如果必要的话更新当前对象数据
by English++小组,englishpp.com

用cookie或者sessionStorage