两个定时器的同步(setInterval)

时间:2013-10-02 10:44:55

标签: javascript jquery setinterval

我正在使用一个脚本,其中包含需要每10分钟调用一次的函数。另外,为了告知用户在重新加载函数之前剩下多少时间(通过AJAX,但现在这不相关),我放了一个小的回归计时器。

这是主要结构:

$(document).ready(function () {

    // Test's function
    function loadDate() {
        var myDate = new Date();
        $("#dateload").html(myDate);
    };

    // Startup Variables
    countermin = 10; // 10 minutes
    countersec = 60;
    minpass = 0;
    secpass = 0

    // Showing info before timer.
    $("#reloadtime").html("Function will be reloaded in " + countermin + "m<b>" + (60 - countersec) + "</b>s<br/>(countersec's value: " + countersec + " - secpass' value: " + secpass + ")");
    $("#timescalled").text("The date function has been called " + minpass + " times  after page's load.");

    loadDate();


    // FIRST setInterval
    // It's our timer.
    intvl1 = setInterval(function () {
        if (countersec++ == 60) {
            countermin--;
            countersec = 1;
        }

        if (countermin < 0) {
            countermin = 9;
            countersec = 1;
            secpass = 0;
        }

        secpass++;

        $("#reloadtime").html("Function will be reloaded in " + countermin + "m<b>" + (60 - countersec) + "</b>s<br/>(countersec's value: " + countersec + " - secpass' value: " + secpass + ")");

    }, 1000);

    // SECOND setInterval
    // Counting 10 minutes to execute the function again (and again...).        
    intvl2 = setInterval(function () {

        minpass++;
        $("#minpass").text("The date function has been called " + minpass + " times after page's load.");
        loadDate();

    }, 600000);

});

我的问题是:两个计时器都没有同步。 intvl2正在执行该函数并在计时器(intvl1)达到0之前返回。错误大约为20秒,每10分钟后增加一次。

如果你比较开始时打印的时间,执行大约6,7分钟,你可以看到与PC时钟相比的时间差异。

如何让它们同步?

您可以查看this fiddle

2 个答案:

答案 0 :(得分:5)

更简单的方法是使用单个计时器来执行这两项操作,更新倒计时信息并触发事件。以下是一个例子:

var oneSecond = 1000;
var counter = 0;

var eventInterval = 5 * oneSecond;
var timesTriggered = 0;

setInterval(function () {

    counter = (counter + oneSecond) % eventInterval;
    $('.countdown').text((eventInterval - counter) / oneSecond);

    if(counter == 0){
        timesTriggered++;
        $('.timesTriggered').text(timesTriggered);
    }

}, oneSecond);

工作小提琴:http://jsfiddle.net/TFDSU/3/

答案 1 :(得分:0)

我已经破解了你的代码 - 我无法忍受10分钟的倒计时:)所以我把它切换到一分钟和其他一些变化,但这工作正常,不会浪费时间..

$(document).ready(function () {
// Function of Test
function loadDate() {
    var myDate = new Date();
    $("#dateload").html(myDate);
};

countersec = 60; //
minpass = 0;

$("#reloadtime").html("Function will be reloaded in "+countersec+"</b>s<br/>");
$("#timescalled").text("The date function has been called " + minpass + " times after page's load.");
loadDate();

intvl1 = setInterval(function () {
    countersec--;
    if (!countersec) { countersec=60; }
    $("#reloadtime").html("Function will be reloaded in "+countersec+"</b>");
}, 1000);

intvl2 = setInterval(function () {

    minpass++;
    $("#minpass").text("The date function has been called " + minpass + " times after page's load.");
    loadDate();

}, 60000);

});