javascript - 在一天的特定时间刷新网页

标签 javascript php jquery wordpress

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

有什么建议吗?

最佳答案

根据 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

关于javascript - 在一天的特定时间刷新网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46099650/

相关文章:

php - 创建带有选项卡的 HTML/CSS/PHP 框架,带有单独的选项卡图像

javascript - 脚本调用后 Css 无法正确呈现

javascript - 可以检测到对 optgroup 标签的操作吗?

javascript - 点击功能在移动设备上无法正常工作

javascript - JS 高阶困惑

Javascript 函数未在表单内呈现

javascript - 寻找 JavaScript RSA 库 2048 位

javascript - 当用户再次滚动到顶部时将图像恢复正常

php - pear.php.net 已关闭。如何安装镜像?

php - 通过php中的函数更新变量值