设置第一次执行的超时

时间:2015-04-06 18:48:49

标签: javascript jquery

我使用此脚本每小时检索一个页面的新内容。

        (function worker() {
            $.ajax({
                url: 'ajax/test.html', 
                success: function(data) {
                    $('.result').html(data);
                },
                complete: function() {
                  setTimeout(worker, 3600000);
                }
            });
        })();

它每60分钟加载一次内容,但我真正想要的是在每小时开始时加载新内容(即8:00再加上9:00)。 如果我在一小时开始加载页面,我的代码是有用的,但如果我在一小时内首次加载它(例如7:45),它会在8:45重新加载内容,依此类推。我怎样才能改进我的代码,说第一次重新加载应该在15分钟后再发生每小时?如何更改setTimeout函数以实现我的目标? 我发现了一段可以接近我的目标的代码,但我仍然不知道如何将它集成到我的函数中:

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);
 }

6 个答案:

答案 0 :(得分:1)

您可以计算到下一个小时的剩余毫秒数

function doNextHour(callback) {
    // time now in milliseconds since 1970
    var now = new Date().getTime();
    var MS_IN_HOUR = 60 * 60 * 1000;
    // remaining ms until next hour
    var remaining = MS_IN_HOUR - (now % MS_IN_HOUR);
    setTimeout(callback, remaining);
}

使用

    (function worker() {
        $.ajax({
            url: 'ajax/test.html', 
            success: function(data) {
                $('.result').html(data);
            },
            complete: function() {
              doNextHour(worker);
            }
        });
    })();

确保您最初还要调用worker()来启动cron作业

答案 1 :(得分:0)

在javascript中,您将获得当前时间的分钟数:

var date = new Date();

var minutes = date.getMinutes();

现在在您的代码中,尝试类似:

if(minutes == 0){

         setTimeout(worker, 3600000);
}else{

        setTimeout(worker, 3600000-minutes*1000);

}

但是如果分钟== 0,请确保第一次获取内容。

答案 2 :(得分:0)

这可能适合你

function worker() {
        $.ajax({
            url: 'ajax/test.html', 
            success: function(data) {
                $('.result').html(data);
            },
            complete: function() {
              setTimeout(worker, 3600000);
            }
        });
    }

var mins = new Date().getMinutes();
if(mins > 0){
  mins = 60 - mins;
}

setTimeout(worker, mins*60*1000);

答案 3 :(得分:0)

使用小时的偏移量进行第一次超时。

setTimeout(worker,3600000 - ((new Date) % 3600000))

这有点准确(Date对象构造可能需要几毫秒)。

答案 4 :(得分:0)

如何重新计算每个循环的延迟:

(function worker() {
    var next_call = 60 - new Date().getMinutes();
    $.ajax({
        url: 'ajax/test.html', 
        success: function(data) {
            $('.result').html(data);
        },
        complete: function() {
          setTimeout(worker, next_call*60*1000);
        }
    });
})();

答案 5 :(得分:0)

我不会直接弄乱Date对象,而是使用这个库:http://momentjs.com/docs/

然后你就会有这样简单的东西,它避免编写自定义日期逻辑,并且易于阅读:

var worker = function() {
  $.ajax({
    url: 'ajax/test.html', 
    success: function(data) {
      $('.result').html(data);
    },
    complete: function() {
      setTimeout(worker, moment().endOf('hour').valueOf());
    }
  });
};

setTimeout(worker, moment().endOf('hour').valueOf());
相关问题