Removing an item in HTML5 Local Storage after a period of time?

前端 未结 4 1108
悲&欢浪女
悲&欢浪女 2021-02-07 14:31

I have a simple HTML5 App that I am currently working on and I\'m wondering if it\'s possible to remove a item in HTML5 Local Storage after a period of time, like: after 24 hour

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-07 14:48

    Use This Solution:

    (function () {
    
      var lastclear = localStorage.getItem('lastclear'),
          time_now  = (new Date()).getTime();
    
      // .getTime() returns milliseconds so 1000 * 60 * 60 * 24 = 24 days
      if ((time_now - lastclear) > 1000 * 60 * 60 * 24) {
    
        localStorage.clear();
    
        localStorage.setItem('lastclear', time_now);
      }
    
    })();
    

提交回复
热议问题