如何逐步加速setInterval?

时间:2014-12-10 20:43:51

标签: javascript jquery setinterval

我在网上运行了倒数计时器,第二个功能是定期检查数据库中的变化,但是当计时器到达时更频繁地检查它(让我们说每3秒钟一次)更重要。计时器启动时结束且不太常见。我有以下代码:

var days, hours, minutes, seconds, check_interval; 

setInterval(function() {

    var current_date = new Date().getTime();
    var seconds_left = (date_end - current_date) / 1000;

    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;

    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;

    minutes = parseInt(seconds_left / 60);
    seconds = parseInt(seconds_left % 60);

    document.getElementById('countdown').innerHTML = days + 'd ' + hours + 'h ' + minutes + 'm ' + seconds + 's';


    if (hours > 0) {
        check_interval = 60000;
    } else if (hours = 0 && minutes > 5) {
        check_interval = 10000;
    } else {
        check_interval = 3000;
    }


    var check_bids = setInterval(function() {
        $.ajax({
            type: 'POST',
            url: '/check.class.php',
            dataType: 'json',
            data: { ... }
        }).done(function(response) {
            ...
        }).fail(function(jqXHR, textStatus, errorThrown) {
            console.log('Bids check failed: '+textStatus, errorThrown);
        });
    }, check_interval); // Accelerating interval

}, 1000);

它可以按我的意愿工作,但我真的不确定我是否可以将这两个setInteval函数组合起来?它有效吗?谢谢你的建议。

更新

感谢Strille,我终于选择了这个只有一个setInterval的简单解决方案:

var days, hours, minutes, seconds; 

setInterval(function() {

    var current_date = new Date().getTime();
    var seconds_left = seconds_to_end = (date_end - current_date) / 1000;

    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;

    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;

    minutes = parseInt(seconds_left / 60);
    seconds = parseInt(seconds_left % 60);

    document.getElementById('countdown').innerHTML = days + 'd ' + hours + 'h ' + minutes + 'm ' + seconds + 's';


    if (hours > 0) { // If time to end is greater than 1 hour repeat ajax call every 60 seconds etc.
        repeat_every_x_seconds = 60;
    } else if (minutes > 5) {
        repeat_every_x_seconds = 10;
    } else {
        repeat_every_x_seconds = 3;
    }

    if (parseInt(seconds_to_end) % repeat_every_x_seconds == 0) {
        check(); // Call this function every x seconds.
    }
}, 1000);



var check = function() {
    $.ajax({
        type: 'POST',
        url: '/check.class.php',
        dataType: 'json',
        data: { ... }
    }).done(function(response) {
        ...
    }).fail(function(jqXHR, textStatus, errorThrown) {
        ...
    });
};

也许对某人有所帮助。抱歉英语:)

1 个答案:

答案 0 :(得分:0)

由于您已经需要每秒运行一些代码来更新文本,因此如果有足够的时间来进行新的ajax调用,则可以在同一个setInterval中每秒进行一次检查。我们只需要添加一个变量last_checked_date来存储我们上次进行ajax调用的时间:

var days, hours, minutes, seconds, check_interval, last_checked_date = 0;

setInterval(function() {

    ... 

    if (hours > 0) {
        check_interval = 60000;
    } else if (hours = 0 && minutes > 5) {
        check_interval = 10000;
    } else {
        check_interval = 3000;
    }

    if (current_date - last_checked_date > check_interval) {
        // do ajax call

        last_checked_date = current_date;
    }

}, 1000);