我想在一天中的特定时间重新加载/刷新(WordPress)网页。因此,您无法使用<?php header("Refresh:60"); ?>
或javascript函数setInterval()
执行确切/特定的时间间隔。
例如,我想在以下时间重新加载页面: 08:30 09:00 09:15 14:20
我想用PHP或jQuery / JavaScript来解决这个问题。
我的代码示例(WordPress):
<?php
// Get Start time and End time from Wordpress using Custom Fields
$sessionTimeStart = get_field('session_time_start');
$sessionTimeEnd = get_field('session_time_end');
$sessionTitle = get_the_title();
$sessionContent = get_the_content();
// Current time
$currentTime = current_time('H:i');
if($currentTime > $sessionTimeStart && $currentTime < $sessionTimeEnd) {
// Prints info about the specific session
echo $sessionTitle;
echo $sessionContent;
}
?>
有什么建议吗?
答案 0 :(得分:1)
基于SO(How to automatically reload a web page at a certain time?)的另一个答案,您可以编写如下函数:
function refreshAt(hours, minutes, seconds)
{
var now = new Date();
var then = new Date();
if ( now.getHours() > hours || (now.getHours() == hours && now.getMinutes() > minutes) || now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >= seconds )
{
then.setDate(now.getDate() + 1);
}
then.setHours(hours);
then.setMinutes(minutes);
then.setSeconds(seconds);
var timeout = (then.getTime() - now.getTime());
setTimeout(function() { window.location.reload(true); }, timeout);
}
然后运行它:
refreshAt(18,45,0); //Will refresh the page at 18:45pm