jQuery auto refresh page on clock time

2019-06-09 09:24发布

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?

5条回答
▲ chillily
2楼-- · 2019-06-09 10:06

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
查看更多
Bombasti
3楼-- · 2019-06-09 10:10

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.

查看更多
冷血范
4楼-- · 2019-06-09 10:18

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);
}
查看更多
狗以群分
5楼-- · 2019-06-09 10:21
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.

查看更多
走好不送
6楼-- · 2019-06-09 10:21
<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);
查看更多
登录 后发表回答