最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set expiration time for data stored in ReactJs localStorage - Stack Overflow

programmeradmin2浏览0评论

I need help on how to clear an Item or localStorage after a certain time even if the website is not currently working. More like a timeout when the localStorage should be cleared. Thank you

I need help on how to clear an Item or localStorage after a certain time even if the website is not currently working. More like a timeout when the localStorage should be cleared. Thank you

Share Improve this question asked Aug 2, 2018 at 15:31 Bello MayowaBello Mayowa 551 gold badge2 silver badges7 bronze badges 1
  • 1 There is no localStorage expiry mechanism, but you could create your own by e.g. storing a timestamp in localStorage and on each page load check if it has expired, or create a timeout for when you will manually remove it. – Tholle Commented Aug 2, 2018 at 15:37
Add a ment  | 

1 Answer 1

Reset to default 6

localStorage does not have support for expiration dates.

You can although save in localStorage a timestamp and next time the app starts up, check the timestamp and if it is too old than clean your app's localStorage.

Another way, if you want to set an expiration date on something, you can try with Cookies

There are some packages that help with this like js-cookie

发布评论

评论列表(0)

  1. 暂无评论