我需要一个函数来初始运行,然后在下午12点和上午12点重新运行。如果有任何影响,我使用VueJS。设置每12小时的时间间隔
fetch_datetime()
{
axios.get('/api/core/datetime').then((response) => {
this.datetime = response.data;
this.set_interval();
});
},
set_interval()
{
var current_date = new Date();
var hours = current_date.getHours();
var minutes = current_date.getMinutes();
var seconds = current_date.getSeconds();
if(hours == 12 && minutes == 0 && seconds == 0 || hours == 0 && minutes == 0 && seconds == 0)
{
this.fetch_datetime();
} else
{
if(hours >= 12)
{
setTimeout(this.fetch_datetime, (1000 * (60 - seconds) * (60 - minutes)) + (1000 * 60 * (24 - hours - 1) * 60));
} else
{
setTimeout(this.fetch_datetime, (1000 * (60 - seconds) * (60 - minutes)) + (1000 * 60 * (12 - hours - 1) * 60));
}
}
但是,这不是按预期方式工作,并且该功能运行得很早,然后在小时内运行多次。
谁将打开浏览器12小时后再次运行该功能? – Pete
这个想法是,如果用户在11:50访问该站点,该功能需要在12:00重新运行。 – Imagine17
更好的问题是:Imagine17真的等了12个小时,看看它是否有效:D – SkryptX