Search code examples
javascripthtmlsettimeoutreload

HTML : Page load every 5 secs and display countdown like 5,4,3,2,1 and then reload the page


Can someone please help me on html reload page for every 5 secs and also i need to have something like below.

In my page, i have "refresh" link, once i click that refresh link, refresh should disappear and it should start showing countdown as 5,4,3,2,1 etc and then it has to reload the same page again, can you please help me.


Solution

  • You can try this:

                var counter = 5;
                var myCounter = setInterval(myFunction, 1000);
    
                function myFunction() {
                  if (counter > 0) {
                    document.getElementById("numbers").innerHTML = counter;
                    counter--;
                  } else {
                    clearInterval(myCounter);
                    document.location.reload(true)
                  }
    
                }
    

    https://jsfiddle.net/9423bvp1/