У меня есть URL-адрес, который вызывается cron-заданием, но я не заставляю его работать.
Я планирую сделать то же самое с помощью Javascript: как я могу перезагрузить страницу таким образом в определенный час дня (8:00 PM)?
Я хотел перезагрузить контроллер с индексной страницы. Я напишу этот скрипт и разместил здесь URL-адрес - Hasif
Поскольку использование Javascript в вашей ситуации ограничено интерфейсом, пользователю нужно будет держать страницу открытой (индекс, как вы упомянули) все время. Это означает, что:
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 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');
Следующий фрагмент кода 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