简体   繁体   English

每小时刷新一次内容/页面

[英]Refresh content/page on the hour every hour

I have a code that is meant to refresh the page on the hour, every hour but it doesn't seem to execute... 我有一个旨在每小时刷新一次页面的代码,但它似乎没有执行...

This is the code: 这是代码:

<?php

date_default_timezone_set('Australia/Sydney');

$date = date("i:s");

list($cur_min, $cur_sec) = explode(':', $date);

$mins_left = ($cur_min == 59) ? 0 : 60 - $cur_min;
$secs_left = 60 - $cur_sec;

$time=($mins_left*60+$secs_left)*1000;

?>

<script type="text/javascript">
setInterval("refresh()",<?php echo $time; ?>);
function refresh(){
    window.location = location.href;
}
</script>

I need it to run off the server clock too and not from when the user lands on the page. 我也需要它来消耗服务器时钟,而不是从用户登陆页面时开始。

Idealy, would be awesome if it could just refresh the content of every div labeled with the class named "locality"... 从理念上讲,如果它可以刷新每个名为“ locality”的标签的div的内容,那将是很棒的...

Try this code for the JavaScript part: 在JavaScript部分尝试以下代码:

function refresh(){
    window.location.reload();
}
setTimeout(refresh, <?php echo $time; ?>);

I'm not sure if it will solve your problem, but I tried a few changes: 我不确定是否能解决您的问题,但是我尝试了一些更改:

  • window.location.reload() is more explicit than window.location = location.href (which, by the way, could be simplified to window.location = window.location ) window.location.reload()window.location = location.href更明确(顺便说一下,可以简化为window.location = window.location
  • setTimeout instead of setInterval because if the refreshing fails again for some reason, you don't want to try refreshing again in setTimeout而不是setInterval因为如果刷新由于某种原因再次失败,则您不想在其中尝试再次刷新
  • put the setTimeout call after the definition of the function to be sure that there isn't a problem with the function not being defined yet setTimeout调用放在函数定义之后,以确保函数尚未定义没有问题

Try it. 试试吧。 If that code still doesn't work for you, try looking at your generated HTML and seeing what value for $time was printed. 如果该代码仍然不适合您,请尝试查看生成的HTML并查看打印$time值。 Also check the browser console for any errors. 还要检查浏览器控制台是否有任何错误。

By the way, the code would be clearer if you renamed the variable $time to $ms_until_start_of_hour . 顺便说一句,如果将变量$time重命名为$ms_until_start_of_hour ,则代码将更加清晰。

I would use DateTime in PHP and for easy convertion use timestamps. 我会在PHP中使用DateTime并为了方便转换而使用时间戳。

$date = new \DateTime(strtotime(time()), new \DateTimeZone("Australia/Sydney")));
$date->add(new \DateInterval('PT1H'));
$time = $date->format('U') % 3600;

And for JS: 对于JS:

var time = (Math.round((new Date()).getTime() / 1000) % 3600);
if(time > <?= $time ?>){
  var rtime = time - <?= $time ?>;
} else {
  var rtime = <?= $time ?> - time; 
}

setInterval(function(){
  window.location.reload(1);
},rtime * 1000);

But im not sure why: 但我不确定为什么:

<meta http-equiv="refresh" content="3600">

Will not suffice as it will just refresh an hour later (and so on) of when the user enters the page, doesn't really matter what timezone. 这还不够,因为它只会在用户进入页面一小时后(依此类推)刷新,实际上与时区无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM