我有一个简单的系统,每隔几秒钟刷新一次div:

$(document).ready(function() {
         $("#nownext").load("response.php");
       var refreshId = setInterval(function() {
          $("#nownext").load('response.php?randval='+ Math.random());
       }, 20000);
    });


现在,由于内容是什么,它更有可能在一个小时或半点时更新。 (尽管并非总是如此)。我想要做的是使系统经常在一个小时之前和之后的几分钟(半点)之间刷新更多,只是为了使其更加精确。

这是否可能/我将如何做而又不会过多地增加客户端计算机的压力?

最佳答案

由于间隔本身将是动态的,因此您将不得不使用setTimeout

像这样(未经测试):

$(document).ready(function() {
    $("#nownext").load('response.php?randval='+ Math.random());
    var minutes = new Date().getMinutes(), interval = 60*10*1000; // default 10 mins
    if (minutes <= 10 || (minutes > 30 && minutes < 35) || minutes > 50) {
        // update once-per-minute if within the first/last 10mins of the hour, or 5mins within the half hour
        interval = 60*1000;
    }
    setTimeout(arguments.callee, interval);
});

关于javascript - Javascript/Jquery刷新计时器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1992127/

10-12 01:36