如何在特定的给定时间自动重新加载或刷新网页?

时间:2014-04-15 18:30:06

标签: php time

我正在使用API​​。问题是这个API会在每60秒钟给出一次结果。它编程为每分钟发送06秒。 所以这将是00:06(MM:SS),01:06,02:06等......

我想有2秒的保证金,所以我想每分钟刷新一次08秒的页面。

我不是程序员而是学习,下面的解决方案非常适合我的使用,但我想这不是正确的方法吗?

是否有任何其他更短的方法来实现此代码的结果:

<?php

$nowis = date('s');

if ( $nowis == "00" ) {$reload = "08" ;} 
if ( $nowis == "01" ) {$reload = "07" ;} 
if ( $nowis == "02" ) {$reload = "06" ;} 
if ( $nowis == "03" ) {$reload = "05" ;} 
if ( $nowis == "04" ) {$reload = "04" ;} 
if ( $nowis == "05" ) {$reload = "03" ;} 
if ( $nowis == "06" ) {$reload = "62" ;} 
if ( $nowis == "07" ) {$reload = "61" ;} 
if ( $nowis == "08" ) {$reload = "60" ;} 
if ( $nowis == "09" ) {$reload = "59" ;} 
if ( $nowis == "10" ) {$reload = "58" ;} 
if ( $nowis == "11" ) {$reload = "57" ;} 
if ( $nowis == "12" ) {$reload = "56" ;} 
if ( $nowis == "13" ) {$reload = "55" ;} 
if ( $nowis == "14" ) {$reload = "54" ;} 
if ( $nowis == "15" ) {$reload = "53" ;} 
if ( $nowis == "16" ) {$reload = "52" ;} 
if ( $nowis == "17" ) {$reload = "51" ;} 
if ( $nowis == "18" ) {$reload = "50" ;} 
if ( $nowis == "19" ) {$reload = "49" ;} 
if ( $nowis == "20" ) {$reload = "48" ;} 
if ( $nowis == "21" ) {$reload = "47" ;} 
if ( $nowis == "22" ) {$reload = "46" ;} 
if ( $nowis == "23" ) {$reload = "45" ;} 
if ( $nowis == "24" ) {$reload = "44" ;} 
if ( $nowis == "25" ) {$reload = "43" ;} 
if ( $nowis == "26" ) {$reload = "42" ;} 
if ( $nowis == "27" ) {$reload = "41" ;} 
if ( $nowis == "28" ) {$reload = "40" ;} 
if ( $nowis == "29" ) {$reload = "39" ;} 
if ( $nowis == "30" ) {$reload = "38" ;} 
if ( $nowis == "31" ) {$reload = "37" ;} 
if ( $nowis == "32" ) {$reload = "36" ;} 
if ( $nowis == "33" ) {$reload = "35" ;} 
if ( $nowis == "34" ) {$reload = "34" ;} 
if ( $nowis == "35" ) {$reload = "33" ;} 
if ( $nowis == "36" ) {$reload = "32" ;} 
if ( $nowis == "37" ) {$reload = "31" ;} 
if ( $nowis == "38" ) {$reload = "30" ;} 
if ( $nowis == "39" ) {$reload = "29" ;} 
if ( $nowis == "40" ) {$reload = "28" ;} 
if ( $nowis == "41" ) {$reload = "27" ;} 
if ( $nowis == "42" ) {$reload = "26" ;} 
if ( $nowis == "43" ) {$reload = "25" ;} 
if ( $nowis == "44" ) {$reload = "24" ;} 
if ( $nowis == "45" ) {$reload = "23" ;} 
if ( $nowis == "46" ) {$reload = "22" ;} 
if ( $nowis == "47" ) {$reload = "21" ;} 
if ( $nowis == "48" ) {$reload = "20" ;} 
if ( $nowis == "49" ) {$reload = "19" ;} 
if ( $nowis == "50" ) {$reload = "18" ;} 
if ( $nowis == "51" ) {$reload = "17" ;} 
if ( $nowis == "52" ) {$reload = "16" ;} 
if ( $nowis == "53" ) {$reload = "15" ;} 
if ( $nowis == "54" ) {$reload = "14" ;} 
if ( $nowis == "55" ) {$reload = "13" ;} 
if ( $nowis == "56" ) {$reload = "12" ;} 
if ( $nowis == "57" ) {$reload = "11" ;} 
if ( $nowis == "58" ) {$reload = "10" ;} 
if ( $nowis == "59" ) {$reload = "09" ;} 

echo $nowis;
echo " - ";
echo $reload;

?>

<meta http-equiv="refresh" content="<?php echo $reload; ?>" >

我不想使用任何Java或其他..请用PHP建议我更好的方法吗?

谢谢

3 个答案:

答案 0 :(得分:1)

这个怎么样?

只是一些数学而不是if

<?php
$nowis = date('s');
$reload = (8 - $nowis + 60) % 60;
echo $nowis;
echo " - ";
echo $reload;
?>
<meta http-equiv="refresh" content="<?php echo $reload; ?>" >

这看起来就像你的长期if声明所做的那样。它似乎给出了相同的结果。

答案 1 :(得分:0)

此代码可以帮助您:

$now = date('s');
$reload = (8 - $now) %  60;
header("refresh: $reload; url=page.php");

答案 2 :(得分:0)

使用javascript的计时器:

// refresh 60 seconds later, only once,
var timeoutId = setTimeout(new function(){location.reload() }, 60 * 1000);

// clear the setTimeout, so that it won't refresh if not yet,
clearTimeout(timeoutId);

// refresh every 20 seconds, repeatly,
var intervalId = setInterval(new function(){location.reload() }, 20 * 1000)

// clear the setInterval, so that it will stop refresh frequently,
window.clearInterval(intervalId)