Once upon a time, localstorage never get expired. But this time, everything has changed except your coding habit. All you need to do is include the expirable-localstorage.js into your web page, and localstorage becomes expirable.
Before: localStorage.setItem('weather_today','Sunny');
Now: localStorage.setItem('weather_today','Sunny',{expires:'1d'});
As the code above indicated, the 3rd argument is extended into native setItem method. You can pass-in the expiration configration and everything will be done in the background.
<script>
var date = new Date()
localStorage.setItem('weather','Sunny',);
</script>
| Key | Value | Expires at |
|---|---|---|
| Expired! Reload page and this localStorage item will be erased. | ||
| Key 'weather' in localStorage is empty. | ||