0

How do I refresh the page automatically every 15 minutes based on clock time?

For example: refresh on 9:00, 9:15, 9:30, 9:45, 10:00, 10:15, so on..

I have seen one similar like I wanted : https://stackoverflow.com/a/1217945/551559 but I don't think it does the job.

setInterval(function(){
  // check clock time on every minute??
  if ( clock_time === '9:15' ) {

  }
},1000);

Can someone give me a solution or any link to look at?

Community
  • 1
  • 1
tonoslfx
  • 3,422
  • 15
  • 65
  • 107
  • What is ```clock_time```? Is this defined somewhere or would you like to determine the current time in JavaScript too (e.g. ```new Date();```)? – mritz_p May 28 '15 at 11:28
  • The Stackoverflow you give is exactely what you need. Just modify it a bit to refresh 15 minutes later once the first timer is done – Michael Laffargue May 28 '15 at 11:31

5 Answers5

10
setInterval(function(){
    var minutes = (new Date()).getMinutes()
    if ( !minutes%15 ) location.reload(); // if minutes is a multiple of 15

},60000); // 60.000 milliseconds = 1 minute

Explaining if(!minutes%15) :

minutes % 15 is a modulo operation. It will divide minutes by 15 and return the rest. So if the result is 0, it means that minutes is a multiple of 15.

Now we need to invert that value : 0 is equivalent to false, so we want !0 (not zero = true)

Finally we get if( ! minutes % 15 ) will be true if minutes is a multiple of 15.

Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
3

I am not sure if you want it to reload after every 15 minutes regardless of when started. But if you need to access the local time and get the current hours and minutes use this:

var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes();

You could check it like this:

setInterval(function(){
  var dt = new Date();
  var clock_time = dt.getHours() + ":" + dt.getMinutes();
  if ( clock_time === '9:15' ) {
     location.reload();
  }
},1000);//or every min, 60000

Hope it helps.

Estarossa
  • 585
  • 4
  • 21
1

You can use the modulus operator to work out if we're in a multiple of 15 minutes

setInterval(function(){
  var Now = new Date();
  if ( Now.getMinutes() % 15 == 0 ) {

  }
},60000); // Run me every minute
Jamie Barker
  • 8,145
  • 3
  • 29
  • 64
  • @Andy Error? What error? This is not the `invalid assignment left-hand side` you're looking for ;) – Jamie Barker May 28 '15 at 11:42
  • 1
    This is not the `invalid assignment left-hand side` I'm looking for. – Andy May 28 '15 at 11:44
  • Or as I used in my answer, `if(!Now.getMinutes()%15)`. – Jeremy Thille May 28 '15 at 11:50
  • @JeremyThille I didn't see yours before I posted mine (I had a distraction here and it was delayed) but I kept mine anyway considering yours is a "code only" answer and doesn't describe how it works. – Jamie Barker May 28 '15 at 11:52
  • Yeah, no worries, I wasn't offended :) Do you think my answer needs more explanation than just the comments I wrote? I think those two lines are pretty self-explanatory. – Jeremy Thille May 28 '15 at 12:12
  • @JeremyThille Unless you understand what modulus is and how it works, you're not going to have a clue what `!minutes%15` does. It's certainly the "short-hand" method of what mine is but it doesn't aid understanding. – Jamie Barker May 28 '15 at 12:18
0

Call this function on page load or via any other trigger, it will set the timeout for the page to reload exactly when it hits xx.[00|15|30|45].00

function quaterlyRelaod() {
  var d = new Date(), min = d.getMinutes(), sec = d.getSeconds();
  var timeout = ((15-min%15)*60-sec)*1000;
  setTimeout(function(){ window.location.reload(); }, timeout);
}
rkrara
  • 442
  • 1
  • 6
  • 17
-2
<meta http-equiv="refresh" content="5"; URL=http://www.yourdomain.com">

If it has to be in the script use set Timeout like

setTimeout(function(){   window.location.reload(1); }, 5000);
Rajesh Kumar
  • 153
  • 2
  • 4
  • 13