问题描述
我的数据是对象
我使用这样的本地存储JavaScript保存它:
I save it use local storage javascript like this :
localStorage.setItem('storedData', JSON.stringify(data))
我只想将这些数据保留1小时.因此,如果超过1小时,则数据将被删除
I just want to keep that data for 1 hour. So if it's been more than 1 hour then the data will be removed
我知道要删除这样的数据:
I know to remove data like this :
localStorage.removeItem("storedData")
但是如何将其设置为1小时后自动删除?
But how can I set it to auto delete after 1 hour?
推荐答案
您不能. HTML5本地存储中的项目何时过期?您唯一可以做的就是在1小时的超时时间内设置delete语句. 这要求用户停留在您的页面上,否则将不会执行超时.
You can't.When do items in HTML5 local storage expire?The only thing you can do is set the delete statement in a timeout of 1 hour. This requires the user to stay on your page or the timeout won't be executed.
您还可以设置一个到期字段.当用户重新访问您的网站时,请先检查其到期时间并删除下次访问时的存储空间.
You can also set an expiration field. When the user revisits your site, check the expiration and delete the storage on next visit as the first thing you do.
这篇关于一小时后如何删除本地存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!