特定时间后刷新页面元素 [英] Refresh page element after specific time

查看:102
本文介绍了特定时间后刷新页面元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是很擅长代码所以请在回答中使用非专业术语!!

I'm not very good at code so please use laymans terms in answering!!

我有广播网站,我隐藏链接直到特定时间(即广播时间)这是我用来执行此操作的代码:

I have broadcast site where I hide links until a specific time of day (i.e. broadcast time) this is the code I use to do this:

<?php
date_default_timezone_set('Europe/London');
$currtime = date('Hi');
// Change time
$starttime = 2200;  
$endtime = 2300; 
$endtime = $endtime + 2400;
$endtest = $currtime + 2400;

?>
<?php

if( $currtime >= $starttime && $endtest <= $endtime ){
?>
<a href="*broadcastlink*">LIVE NOW</a>
<?php
}
else{
?>
Live at 22:00
<?php
}
?>

目前用户必须手动刷新页面,这有点乱。

Currently users have to refresh the page manually which is a bit messy.

我知道这可以使用javascript轻松完成每x秒刷新整个页面但我不想这样做因为我有另一段代码显示facebook like弹出窗口除非他们点击,否则不会关闭5分钟,因此刷新整个页面会不断显示弹出窗口。

I know this could be done easily using javascript to refresh the whole page every x seconds but I don't want to do this as I have another piece of code that shows a "facebook like" popup window that doesn't close for 5 minutes unless they click like, so refreshing the whole page would constantly show the popup window.

请有人写一个脚本以便我可以将刷新脚本链接到上面的php脚本,以便在链接显示容器刷新的时间,我环顾四周,发现到目前为止没有什么合适的?

Please can someone write a script so that I can link a refresh script to the php script above so that at the link reveal time the container refreshes, I've looked around and found nothing suitable so far?

提前谢谢任何帮助 - 非常感谢

Thanks in advance for any help - much appreciated

推荐答案

坏消息是PHP无法强制浏览器做任何事情。你需要javascript与ajax来实现这一目标。但如果在客户端禁用了javascript,该怎么办? php中有一个小技巧。

bad news is PHP cannot force browser to do anything. you need javascript with ajax to achieve this. but what if javascript is disabled in client side. there is a small trick in php .

这是你可以做的。

确定秒数通过直播开始直播

determine the number of seconds for the live broadcast to start by doing

$numOfSeconds = $starttime - $currtime;

然后一旦你知道你想要刷新浏览器多少秒,你就可以这样做了用php。

then once you know after how many seconds you want to refresh your browser here is how you can do it with php.

header( "refresh:$numOfSeconds;urltoanywhere.php" );

这将带您到特定时间后您想去的任何网址。如果要刷新,请将URL定义到同一页面。你可以扩展这个逻辑来实现你想要的方式。

this will take you to any url you wish to go after specific time. define the url to the same page if you wish to refresh. and you could extend this logic to implement the way you want to.

Update1:​​

以下是我测试过的工作代码。希望这可以帮助你:)

Below is the working code which i have tested. hope this helps you :)

<?php
ob_start();
date_default_timezone_set('Europe/London');
$currentPage = $_SERVER['PHP_SELF'];
$currentTimestamp = strtotime(date('Y-m-d H:i:s'));
//Define your startTime here in {Year-Month-Day Hour:Minute:Second} format
$startTime = '2012-04-26 21:57:00';
//Define your endTime here in {Year-Month-Day Hour:Minute:Second} format.
$endTime = '2012-04-27 22:00:00';
$startTimestamp = strtotime($startTime);
$endTimestamp = strtotime($endTime);
$numOfSecondsToReload = $startTimestamp - $currentTimestamp;
if($currentTimestamp >= $startTimestamp && $currentTimestamp <= $endTimestamp):
?>
<a href="*broadcastlink*">LIVE NOW</a>
<?php else: ?>
<p>Live at <?php echo date('H:i', $startTimestamp); ?></p>
<?php header( "refresh:$numOfSecondsToReload;$currentPage"); ?>
<?php endif; ?>

Update2:重新加载特定的div内容。这是您可以使用的代码。

Update2: to reload specific div content. here is the code you can use.

<div id="live">
    <?php
    ob_start();
    date_default_timezone_set('Europe/London');
    $currentPage = $_SERVER['PHP_SELF'];
    $currentTimestamp = strtotime(date('Y-m-d H:i:s'));
    $startTime = '2012-04-27 12:42:20';
    $endTime = '2012-04-28 22:00:00';
    $startTimestamp = strtotime($startTime);
    $endTimestamp = strtotime($endTime);
    $numOfSecondsToReload = $startTimestamp - $currentTimestamp;
    if($currentTimestamp >= $startTimestamp && $currentTimestamp <= $endTimestamp):
    ?>
    <a href="*broadcastlink*">LIVE NOW</a>
    <?php else: ?>
    <p>Live at <?php echo date('H:i', $startTimestamp); ?></p>
    <?php endif; ob_end_flush(); ?>
    <div id="timeremaining" hidden><?php echo $numOfSecondsToReload * 1000; ?></div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
    var numOfMiliSecondsToReload = $('#timeremaining').html();
    var timeToReload = (numOfMiliSecondsToReload >= 0) ? numOfMiliSecondsToReload : 86400000;
    $(function() {
        $(function() { setTimeout( reloadDiv, timeToReload ); });
        function reloadDiv() { $('#live').load(location.href);}
    });
</script>

希望这可以帮到你。

这篇关于特定时间后刷新页面元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆