【发布时间】:2017-07-25 12:03:01
【问题描述】:
我需要一个函数来开始运行,然后在中午 12 点和上午 12 点重新运行。如果有任何影响,我正在使用 VueJS。
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 小时以再次运行该功能?
-
思路是如果用户在11:50访问该站点,该函数需要在12:00再次运行。
-
更好的问题是:Imagine17 真的等了 12 个小时才知道它是否有效:D
-
它很容易测试。正如我所说,该函数会提前运行,然后每小时运行多次。
-
我认为我们遗漏了一些东西。 fetch_datetime() 里面有什么?
标签: javascript jquery vue.js