最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery auto refresh page on clock time - Stack Overflow

programmeradmin1浏览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 : 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?

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./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?

Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked May 28, 2015 at 11:25 tonoslfxtonoslfx 3,44216 gold badges68 silver badges107 bronze badges 2
  • 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 Commented May 28, 2015 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 Commented May 28, 2015 at 11:31
Add a ment  | 

5 Answers 5

Reset to default 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.

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.

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

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);
}
<meta http-equiv="refresh" content="5"; URL=http://www.yourdomain.">

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

setTimeout(function(){   window.location.reload(1); }, 5000);
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>