Refresh page element after specific time

社会主义新天地 提交于 2019-12-24 10:46:50

问题


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.

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.

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


回答1:


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 .

here is what you can do.

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

$numOfSeconds = $starttime - $currtime;

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" );

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: 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>

hope this helps you.




回答2:


No one is going to do you work for you, so don't wait for someone to write your script for you.

Since you know at what time you should hide some links, then you can calculate the time it takes for you to hide them. Do something like this:

  • User makes a request at 11:50, but you plan to hide the links at 12:00, in ten minutes
  • Your server calculates the amount of seconds between 11:50 and 12:00. Take a look at time() and strtotime() functions.
  • Your server returns the page to the user and also returns the amount of seconds until links should be hidden. You could return that data as a hidden form value
  • Then, in Javascript, you use setTimeout() method to call JavaScript function that hides the links in X amount of time (the amount of seconds until 12:00).
  • If you wrote the amount of seconds as a hidden value like explained above, then you can fetch that value in JavaScript with document.getElementById('hidden_form_value').value.


来源:https://stackoverflow.com/questions/10328427/refresh-page-element-after-specific-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!