每小时呼叫一次功能

时间:2013-11-07 21:51:03

标签: javascript

我正在尝试更新我页面上的气象服务信息。 信息应该每小时更新一次。 我怎样才能每小时在一小时内调用一个函数?

我有点想法但我不确定如何实际改进它以便它起作用...... 我想到的就是创建一个if语句,例如:(伪代码)

//get the mins of the current time
var mins = datetime.mins();    

if(mins == "00"){
    function();
 }

6 个答案:

答案 0 :(得分:17)

您想查看setIntervalhttps://developer.mozilla.org/en-US/docs/Web/API/Window.setInterval

用你的代码告诉你要调用的内容有点难,但它会以下列形式出现:

function callEveryHour() {
    setInterval(yourFunction, 1000 * 60 * 60);
}

如果您每小时都需要,请尝试以下方法:

var nextDate = new Date();
if (nextDate.getMinutes() === 0) { // You can check for seconds here too
    callEveryHour()
} else {
    nextDate.setHours(nextDate.getHours() + 1);
    nextDate.setMinutes(0);
    nextDate.setSeconds(0);// I wouldn't do milliseconds too ;)

    var difference = nextDate - new Date();
    setTimeout(callEveryHour, difference);
}

现在,此实现检查一次的时间,设置延迟(或立即调用函数),然后依赖setInterval在此之后跟踪。另一种方法可能是每x秒/分钟轮询时间,然后触发它.getMinutes() == 0(类似于if语句的第一部分),这可能会牺牲(边际)性能(边际)精度。根据您的具体需求,我会使用这两种解决方案。

答案 1 :(得分:7)

工作样本:

http://jsfiddle.net/mgX6e/

function tick()
{
    //get the mins of the current time
    var mins = new Date().getMinutes();
    if(mins == "00"){
        alert('Do stuff');
     }
    console.log('Tick ' + mins);
}

setInterval(tick, 1000);

答案 2 :(得分:7)

你可能想要的是这样的:

var now = new Date();
var delay = 60 * 60 * 1000; // 1 hour in msec
var start = delay - (now.getMinutes() * 60 + now.getSeconds()) * 1000 + now.getMilliseconds();

setTimeout(function doSomething() {
   // do the operation
   // ... your code here...

   // schedule the next tick
   setTimeout(doSomething, delay);
}, start);

所以基本上第一次用户获得访问权限时,您需要知道下一个“小时”的毫秒延迟是多少。因此,如果用户在8:54(56秒和123毫秒)访问该页面,则必须在大约3分钟后安排第一次执行:在完成第一次执行后,您可以每隔“小时”调用一次( 60 * 60 * 1000)。

答案 3 :(得分:1)

编辑:糟糕,我没有看到“o”时钟的东西,所以我编辑了我的答案:

var last_execution = new Date().getTime();
function doSomething(force){
  var current_time = new Date().getTime();
  if (force || (current_time.getMinutes() == 0)
  {
    last_execution = current_time;
    // something
    // ...
  }
  setTimeout(doSomething(false), 1000);
}
// force the first time
doSomething(true); 

答案 4 :(得分:0)

// ... call your func now
let intervalId;
let timeoutId = setTimeout(() => {
  // ... call your func on end of current hour
  intervalId = setInterval(() => {
     // ... call your func on end of each next hours
  }, 3600000);
}, ((60 − moment().minutes()) × 60 × 1000) - (moment().second() * 1000));

答案 5 :(得分:0)

在一小时后的特定分钟重复

这个计数器有点用途更广;它允许重复执行一项任务总是在整点后的同一分钟(例如整点后的 37 分钟),并且精度高达毫秒。

这个计时器的精度来自它的递归。 在每次递归时,都会重新计算到下一分钟的毫秒时间。这可以防止长时间的时间延迟。

% 符号指的是 modulo operator

function minuteCount(minutesAfterHour) {

    const now          = new Date();
    const hours        = now.getHours();
    const minutes      = now.getMinutes();
    const seconds      = now.getSeconds();
    const milliseconds = now.getMilliseconds();

    waitUntilNextMinute = setTimeout(minuteCount, 60000 - seconds * 1000 - milliseconds);

    if(minutes % 60 === minutesAfterHour) {
        doSomethingHourly();
    }

}

minuteCount(37);

最后,定时器最好远离主线程。它们最好在 web worker 内运行,如 explained here。 这与桌面浏览器中未聚焦的标签完美配合。

但是,Android 版 Chrome 上的专用网络工作者会在将主客户端移至后台约 5 分钟后进入睡眠

相关问题