Javascript/Jquery刷新计时器 [英] Javascript/Jquery Refresh timer

查看:82
本文介绍了Javascript/Jquery刷新计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple system that refreshes a div every few seconds:

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

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

Now, because of what the content is, it is more likely to update on the hour, or at half past. (Though not always). What I'd like to do it make the system refresh MORE often between a few minutes before and after the hour (and half past), just to make it more precise.

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

Is this possible/how would I do it without stressing out the client's computer too much?

推荐答案

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

Since the interval itself is going to be dynamic, you're going to have to use setTimeout instead.

类似的东西(未经测试):

Something like this (untested):

$(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/Jquery刷新计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆