简体   繁体   English

jQuery计时器倒数数组

[英]jquery array of timers countdown

i have this JSFiddle that has the script to do the multiple timers, but only the last one works, i'd searched a lot about this, and didn't find an answer. 我有一个JSFiddle ,该脚本具有执行多个计时器的脚本,但是只有最后一个有效,我对此进行了很多搜索,但未找到答案。

I suppose that the problem is in the array of timers, but not sure, can you help? 我想问题出在定时器数组中,但是不确定,您能帮上忙吗?

jQuery.fn.countdown = function (yr, mes, d, h, m, s) {
    $that = $(this);
var delta = 0;

var start = function (yr, mes, d, h, m, s) {
    theyear = yr;
    themonth = mes;
    theday = d;
    thehour = h;
    theminutes = m;
    theseconds = s;

    var today = new Date();
    var todayy = today.getYear();
    if (todayy < 1000) todayy += 1900;
    var todaym = today.getMonth();
    var todayd = today.getDate();
    var todayh = today.getHours();
    var todaymin = today.getMinutes();
    var todaysec = today.getSeconds();

    var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec;

    var futurestring = montharray[mes - 1] + " " + d + ", " + yr + " " + thehour + ":" + theminutes + ":" + theseconds;

    dd = Date.parse(futurestring) - Date.parse(todaystring) + delta;

    dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1);
    dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
    dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
    dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);

    if (dday < 0 && dhour < 0 && dmin < 0 && dsec < 1) {
        alert("ended");
        return;
    } else {
        $that.html("<span><strong>" + dday + "</strong><span> Days | " + "<span><strong>" + dhour + "</strong></span> Hours | " + "<span><strong>" + dmin + "</strong></span> Minutes");
    }

    setTimeout(function () {
        start(theyear, themonth, theday, thehour, theminutes, theseconds);
    }, 1000);
}
return {
    start: function () {
        start(yr, mes, d, h, m, s);
    },
    addTime: function (ms) {
        delta += ms;
    }
}

}; };

this is what makes the countdown. 这就是倒计时的原因。 check the rest in jsfiddle 检查jsfiddle中的其余部分

The problem is that you are ommiting var keyword when declaring variables, thus they are declared as global and shared between timers. 问题是声明变量时省略了var关键字,因此将它们声明为全局变量并在计时器之间共享。 This should work ( jsfiddle ): 这应该工作( jsfiddle ):

var $that = $(this);

... ...

var theyear = yr;
var themonth = mes;
var theday = d;
var thehour = h;
var theminutes = m;
var theseconds = s;

... ...

var dd = Date.parse(futurestring) - Date.parse(todaystring) + delta;

var dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1);
var dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
var dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
var dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM