使localStorage或sessionStorage像Cookie一样过期 [英] make localStorage or sessionStorage expire like cookies

查看:160
本文介绍了使localStorage或sessionStorage像Cookie一样过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


<强>可能重复:结果,
HTML5本地存储中的项目何时到期?


我的web应用程序使用客户端存储( sessionStorage localStorage )。但是,我希望此数据在一段时间后(例如2天)过期。 cookies 过期日期是一项超级简单的任务。但我不知道如何设置HTML5存储的到期日期。



任何想法如何解决这个问题?效率和简单性是非常重要的。


解决方案

你能做的最好是设置在存储时间戳,如果用户访问您的在指定的时间段之后,您可以删除存储的数据。

sessionStorage 应该以相同的方式工作,除非你的定时到期时间不够长。在这种情况下,它会很快到期。


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.

解决方案

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.

这篇关于使localStorage或sessionStorage像Cookie一样过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆