Javascript/Jquery刷新计时器

Javascript/Jquery刷新计时器,javascript,jquery,setinterval,Javascript,Jquery,Setinterval,我有一个简单的系统,每隔几秒钟刷新一次div: $(document).ready(function() { $("#nownext").load("response.php"); var refreshId = setInterval(function() { $("#nownext").load('response.php?randval='+ Math.random()); }, 20000); }); 现在,由于

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

$(document).ready(function() {
         $("#nownext").load("response.php");
       var refreshId = setInterval(function() {
          $("#nownext").load('response.php?randval='+ Math.random());
       }, 20000);
    });
现在,由于内容的不同,它更有可能在一小时或半小时更新。(尽管并非总是如此)。我想做的是让系统在一小时前几分钟和一小时后几分钟(以及半小时)之间更频繁地刷新,只是为了让它更精确


这是否可能/我如何在不给客户端的计算机施加太多压力的情况下做到这一点?

由于间隔本身是动态的,因此您必须使用
setTimeout

类似这样(未经测试):

$(文档).ready(函数(){
$(“#nownext”).load('response.php?randval='+Math.random());
var minutes=new Date().getMinutes(),interval=60*10*1000;//默认值为10分钟
如果(30分钟和<35分钟)| |分钟>50分钟){
//如果在一小时的前10分钟内或半小时内5分钟内,则每分钟更新一次
间隔=60*1000;
}
setTimeout(arguments.callee,interval);
});

使用setTimeout而不是setInterval,这样您可以动态更改下一个间隔的计时。我不确定在“快速”时间段内创建和检查Date()对象(以毫秒为单位)会对性能产生什么影响,但如果出现问题,您可以随时将频率调整到更接近每秒的频率

start_timer = function(timing) {
   var timer, d = new Date(), min = d.getMinutes(),
     timeout = 20000; /* slow timeout */
   if((min >= 28 && min <= 30) || min >= 58) {
     timeout = 100; /* fast timeout */
   }
   timer = setTimeout(start_timer, timeout);
   // Call your code here.
};

$(document).ready(function() {
    start_timer();
});
start\u timer=功能(定时){
变量计时器,d=new Date(),min=d.getMinutes(),
超时=20000;/*慢速超时*/
如果((最小值>=28&&min=58){
超时=100;/*快速超时*/
}
定时器=设置超时(启动定时器,超时);
//在这里调用您的代码。
};
$(文档).ready(函数(){
启动计时器();
});

我同意这一点,但他的初始间隔是20秒,他要求更频繁的近半小时休息。我唯一要注意的是“拍自己”,所以我会添加一些“进行中”状态变量。
start_timer = function(timing) {
   var timer, d = new Date(), min = d.getMinutes(),
     timeout = 20000; /* slow timeout */
   if((min >= 28 && min <= 30) || min >= 58) {
     timeout = 100; /* fast timeout */
   }
   timer = setTimeout(start_timer, timeout);
   // Call your code here.
};

$(document).ready(function() {
    start_timer();
});