简体   繁体   中英

Set sessionStorage expiration

I have this code which sets a sessionStorage. It works fine but I would like to set an expiration date of 24 hours . Can I do this? I know this can be done with cookies but not sure with sessionStorage objets.

<script>
  
  var pageViewed = parseInt("1");
  
  if(sessionStorage.pageViewed)
  {
    var intSessionStoragePageViewed = parseInt(sessionStorage.pageViewed);
    pageViewed =  intSessionStoragePageViewed + 1;
    sessionStorage.setItem("pageViewed", pageViewed);
  }
  else if(!sessionStorage.pageViewed)
  {
    
    sessionStorage.setItem("pageViewed", pageViewed);
  }
  
  </script>

localStorage and sessionStorage do not have built-in expiration dates like cookies do. sessionStorage is erased at the end of the browser session; localStorage is erased only when you or the end user explicitly erases it.

If you want locally stored data to expire after a specific duration, use cookies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM