How do I update a page at a set time?

Asked

Viewed 36 times

1

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

I tried to do that...

<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>

setTimeout only cycle once?

  • 1

    What you’ve tried to do?

  • I’m editing.

  • I do not know who had commented here ""Trade by setInterval", it worked! Thanks!

2 answers

2

I was able to solve the problem using setInterval, instead of setTimeout.

Stayed 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>

2


The setTimeout is only executed 1 time. If you want it to be uninterrupted, exchange for setInterval.

Now a problem arises: as the cycle is run every 3 seconds, it will re-load the page every 3 seconds when the minute meets one of the conditions of the if. What you can do is create a localStorage to store the value of the minute so that Reload only occurs 1 time, and put one more condition in the if checking if the localStorage is different from the current minute, and including the conditions || in 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 that no there are problems in creating localStorage (although I’ve seen comments from that the localStorage is synchronous, but I’m not sure and I found no documentation about it).

  • Beauty! Thank you for the answer! I will test and already return!

  • The first time he updates, updates only once, as he said, but when he updates the second time, he updates several times, I think the value of the Torage locale is not updating. always getting different

  • 1

    Ready. Missing include the conditions || in brackets.

  • It worked! Thank you Sam!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.