How to set the key storage period? How to prevent the appearance of a modal window after closing and then opening the browser?

if (localStorage.getItem("story") !== 'true') { localStorage.setItem("story", "true"); // Calling the bootstrap modal window.setTimeout(function () { $("#Modal").modal(); }, 6000); $('.modal button').on('click', function(){ $('.step-block').remove(); $('.modal .modal-body').append(initPassInterview) $('.modal .share').on('click', function(){ window.setTimeout(function () { $("#Modal").modal('hide'); }, 3000); }) }) } 
  • one
    Make an object with a variable and timestamp, convert to json, write json array to localStorage. When you open the page, parse the json array and compare the timestamp with the current time. - EatMyDust

1 answer 1

The retention period cannot be specified for localStorage, the user and browser themselves determine this. You can use cookies for this, here is a convenient lib.

 if (Cookies.get('story') !== 'true') { // 7 дней, 1/48 поставит на 30 мин, подробнее тут https://github.com/js-cookie/js-cookie/wiki/Frequently-Asked-Questions#expire-cookies-in-less-than-a-day Cookies.set('story', 'true', { expires: 7, path: '' }); // Calling the bootstrap modal window.setTimeout(function () { $("#Modal").modal(); }, 6000); $('.modal button').on('click', function(){ $('.step-block').remove(); $('.modal .modal-body').append(initPassInterview) $('.modal .share').on('click', function(){ window.setTimeout(function () { $("#Modal").modal('hide'); }, 3000); }) }) } 

You can delete it this way: Cookies.remove ('story');