user15375057
user15375057

Reputation:

I want to reload a webpage in a loop in javascript

I want to reload a page in loop but the issue is when I just put reload in an infinite loop it starts reloading but never finish a single reload. So, I used setTimeout but, it reloads page only once and console also get reloaded.

function reload_p() {
  window.location.reload(1);
}

let n = 3;
while (n--) {
  setTimeout(function () {
    reload_p();
  }, 5000);
}

Upvotes: 0

Views: 760

Answers (1)

Reda EL FILLALI
Reda EL FILLALI

Reputation: 103

You can use session

if(!sessionStorage.getItem('counter')) {
   sessionStorage.setItem('counter', 3);
}

let n = sessionStorage.getItem('counter');

if(n !== 0) { 
   window.location.reload(1);
} else {
   sessionStorage.removeItem('counter');
}

Upvotes: 2

Related Questions