Search code examples
jqueryreloadcountdown

page reload jquery countdown


I have the following code..

<script>
    $(document).ready(function () {
        /* Refresh every minute */
        setInterval(function () { cache_clear() }, 60000);
    });
    function cache_clear() {
        window.location.reload(true);
    }
</script> 

It reload the page every minute, it would be nice to have a countdown function added to this so the user can see how long to next reload. But I could use some help with that.


Solution

  • You can maintain a variable and decrement it. Use interval as 1000 milliseconds, which is 1 second. Once the counter is 0, call cache_clear

    $(document).ready(function(){
       var count = 60;
       setInterval(function(){
          $('body').text("Reload in " + (count--) + "secs");
          if(count == 0) cache_clear();
       }, 1000)
    });
    

    DEMO