How do I update a page at a set time?

1

I need to refresh a page every 15 minutes, but with a set time (HH: 00 | HH: 15 | HH: 30 | HH: 45).

I tried to do this ...

<script>

        $(document).ready(function () {
            // Handler for .ready() called.
            window.setTimeout(function () {
                var data = new Date();
                var minute = data.getMinutes();
                if(minute == 15 || minute == 30 || minute == 45 || minute == 0){
                    location.href = "NewFile.jsp";
                }
            }, 3000);
        });

    </script>

Does setTimeout only cycle once?

    
asked by anonymous 07.12.2018 / 12:44

2 answers

2

The setTimeout only runs 1 time. If you want to be uninterrupted, switch to setInterval .

Now a problem arises: as the cycle is run every 3 seconds, it will reload the page every 3 seconds when the minute meets one of the if conditions. What you can do is to create a localStorage to save the minute value so that reload only occurs 1 time, and putting another condition in if by checking if the localStorage is different from the current minute, and include the conditions || within parentheses () :

$(document).ready(function () {
   // Handler for .ready() called.
   window.setInterval(function () {
       var data = new Date();
       var minute = data.getMinutes();
       if((minute == 15 || minute == 20 || minute == 45 || minute == 0) && localStorage.getItem("reload") != minute){
          localStorage.setItem("reload", minute);
          setTimeout(function(){
             location.href = "teste.html";
          }, 500);
       }
   }, 3000);
});
  

See that I used a setTimeout before reloading the page so it would not   there are problems with creating the localStorage (although I've seen   that the localStorage is synchronous, but I'm not sure and did not find documentation about it.)

    
07.12.2018 / 13:21
2

I was able to solve the problem using setInterval, rather than setTimeout.

It looks like this:

<script>

        $(document).ready(function () {
            window.setInterval(function () {
                var data = new Date();
                var n = data.getSeconds();
                var minute = data.getMinutes();

                if(minute == 15 || minute == 20 || minute == 45 || minute == 0){
                    if(n < 6){
                        location.href = "NewFile.jsp";
                    }
                }
            }, 3000);
        });

</script>
    
07.12.2018 / 13:19