拍卖倒计时jQuery

时间:2014-01-27 16:31:02

标签: javascript jquery timer countdown

我正在尝试为我的网络应用程序进行拍卖倒计时,但唯一有效的倒计时是最后一次,我怎么能解决这个问题,我试图使用浏览器断点,但还没有结果。< / p>

        function deployTimersSearch() {
        var nb = $("div.productFull").find("div.data_exptag").length;

        var dia = "";
        var mes = "";
        var ano = "";
        var hora = "";
        var minutos = "";
        var segundos = "";

        var arrayContadores = new Array(nb);

        for (var i = 0; i < nb; i++) {
            dia = $("div.productFull").find("div#diatag" + i).text();
            mes = $("div.productFull").find("div#mestag" + i).text();
            ano = $("div.productFull").find("div#anotag" + i).text();
            hora = $("div.productFull").find("div#horatag" + i).text();
            minutos = $("div.productFull").find("div#minutotag" + i).text();
            segundos = "00";

            arrayContadores[i] = $("div#countDowntag" + i).countdown(ano, mes, dia, hora, minutos, segundos);
            arrayContadores[i].start();
        }

    }

直到这里工作精细

我认为问题出在这里:

        <script>
        var before = "";
        var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
        var theyear, themonth, theday, thehour, theminutes, theseconds;

        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;
                }
            }
        };
        </script>

1 个答案:

答案 0 :(得分:0)

问题是你在声明变量时忽略了var关键字,因此它们被声明为全局并在定时器之间共享。这应该有效(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);