gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:31:04 25 4
gpt4 key购买 nike

我想在一天中的特定时间重新加载/刷新 (WordPress) 网页。因此,您可以使用 <?php header("Refresh:60"); ?> 执行没有确切/特定的间隔。或 javascript 函数 setInterval() .

例如,我只想在这些时候重新加载页面:08:3009:0009:1514: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com