我每天都使用以下函数来尝试刷新我的页面:
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); console.log("reloading"); }, timeout);
}我这样调用...which
refreshAt(00,01,00);然而,在我的Mac上的Chrome浏览器中,它永远不会刷新。我在我的脚本顶部附近有一个调用,它在主体中。声明位于脚本的底部。在我的Android手机上,如果我白天在标签之间切换,它会刷新,但不正确,在我添加刷新功能之前,后者效果更好。我很困惑。
发布于 2018-01-19 14:16:00
这真的取决于您正在处理的应用程序的类型。如果是网站或应用程序,页面将在用户每次打开时刷新。但是,如果您正在处理的应用程序总是显示在没有用户操作选项的情况下,那么您可以设置一个CRON作业。这也取决于你正在使用的框架类型,angular,react等等。然而,使用纯javascript,你可以设置一个每天在特定时间运行的作业。这是一个我认为有用的库的链接。https://www.npmjs.com/package/cron
var CronJob = require('cron').CronJob;
var job = new CronJob('0 7 * * *', function() {
/*
*+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * *
*/
}, function () {
/* This function is executed when the job stops */
},
true, /* Start the job right now */
timeZone /* Time zone of this job. */
);您可以查看文档并创建自己的工作。
发布于 2018-01-19 14:45:53
希望这能起作用。
function refreshAt(hours, minutes, seconds)
{
var now = new Date(), then = new Date();
then.setHours(hours,minutes,seconds,0);
if(then.getTime()<now.getTime())
{
then.setDate(now.getDate() + 1);
}
var timeout = (then.getTime() - now.getTime());
setTimeout(function() { window.location.reload(true); }, timeout);
}
refreshAt(0,1,0);
https://stackoverflow.com/questions/48335020
复制相似问题