【问题标题】:Refresh a webpage at specific times of a day在一天中的特定时间刷新网页
【发布时间】:2017-09-07 15:02:48
【问题描述】:

我想在一天中的特定时间重新加载/刷新 (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;
    }
?>  

有什么建议吗?

【问题讨论】:

标签: javascript php jquery wordpress


【解决方案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

【讨论】:

  • 您从另一个答案中完全复制了一个答案并将其发布为您自己的答案,而只是提到它来自另一个答案但实际上并没有链接到它,这似乎有点不公平。似乎如果答案几乎完全相同,则应该将其标记为重复,是吗?对于未来的读者,原始答案发布在here
  • 非常真实@GrumpyCrouton,我已经更新了答案。我会认为自己被告知,将来会链接到答案。
猜你喜欢
  • 2021-02-17
  • 1970-01-01
  • 2021-07-22
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 1970-01-01
相关资源
最近更新 更多