我有一个由cron作业调用的URL,但是我无法使其正常工作。
我打算使用Javascript做同样的事情:如何在一天的特定时间(晚上8:00)以这种方式重新加载页面?
答案 0 :(得分:0)
我想从索引页面重新加载控制器。我将编写此脚本并将URL放在此处– Hasif
由于在您的情况下只能在前端使用Javascript,因此用户需要始终保持页面处于打开状态(如您所述 index )。这意味着:
var td =new Date(), date2store = `${td.getUTCFullYear()}-${td.getUTCMonth().toString().padStart(2,0)}-${td.getUTCDate().toString().padStart(2,0)} ${td.getUTCHours().toString().padStart(2,0)}:${td.getUTCMinutes().toString().padStart(2,0)}`;
alert('Cookie to store: last_date=' + date2store + ' --> Is it after 8 PM UTC? ' + (new Date(date2store).getUTCHours() >= 19 ? 'YES!' : 'NO!' ));
// place the code below in a setInterval(function() {...}, 1000*60*60);
// the if below should also the test the current cookie's datetime as the first condition
// 0 index hour exists (therefore compare with 19 instead of 20)
if(new Date().getUTCHours() >= 19) {
alert('Past 8 PM');
// save the current date and time in the cookie HERE
// reload the index page
// window.location.reload(true); // true for not using cache
// OR redirect to a new location
// window.location.href = 'https://...';
} else alert('Not 8 PM yet');
答案 1 :(得分:-1)
以下JavaScript代码段将允许您在给定的时间刷新:
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()函数。
refreshAt(15,35,0); //Will refresh the page at 3:35pm