JavaScript: How to reload page twice

94 Views Asked by At

On my site I want reload my page twice: After 20 seconds of being on the page and after 50 seconds.

This is the code I use:

<script>
(function()
{
  if( window.localStorage)
  {
    if( !localStorage.getItem('firstLoad') )
    {
      localStorage['firstLoad'] = true;
      setTimeout(function(){ location.reload(); }, 20000)
    }  
    else    
     localStorage.removeItem('firstLoad');
      
  }
})();
</script>

But the page is reloaded only once after 20 seconds. What I can do to refresh the page again after 50 sec?

1

There are 1 best solutions below

2
hamza muzafar On

if (window.localStorage) {
  // Reload the page once
   if(localStorage['firstLoad'] = true)
      location.reload();
   setTimeout(() => {
    // Reload the page a second time
   location.reload();
  }, 50000); // 50000 milliseconds = 50 seconds
} else {
  localStorage.removeItem('firstLoad');
}