我的JQuery倒计时效果不佳,

时间:2016-10-26 09:03:47

标签: javascript jquery html countdown

我想让jQuery计时器倒计时。它应该有天,日,月,小时,秒。我使用下面的代码,但它不起作用。刷新浏览器时,计数器正在重新启动。请帮我纠正这段代码。

这是jsFiddle https://jsfiddle.net/saifudazzlings/LfLdo381/

js代码:

var sec = 60;
var min = 100;
var hr = 24;
var updateTimer = function() {
    var timer = localStorage.getItem('timer') || 0;
    var timermin = localStorage.getItem('timermin') || 0;
    var timerhr = localStorage.getItem('timerhr') || 0;
    $("div#timermin").html(timermin);
    $("div#timerhr").html(timerhr);

    if (timer === 0) {
        $("div#timer").html("00");
    } else if (timer <= 1) {
        timer--;
        timermin--;
        localStorage.setItem('timermin', timermin);
        $("div#timermin").html(timermin);
        if (timermin < 1) {
            if (timerhr == 0) {
                localStorage.removeItem('timermin', timermin);
                $("div#timermin").html("00");
                localStorage.removeItem('timer', timer);
                $("div#timer").html("00");
                localStorage.removeItem('timerhr', timerhr);
                $("div#timerhr").html("00");
            } else {
                timerhr--;
                localStorage.setItem('timerhr', timerhr);
                $("div#timerhr").html(timerhr);
                localStorage.setItem('timermin', min);
                $("div#timermin").html(timermin);

            }
            //timerhr--;
            //localStorage.setItem('timerhr', timerhr);
            //$("div#timerhr").html(timerhr);
            //localStorage.setItem('timermin', min);
            //$("div#timermin").html(timermin);
        }

        localStorage.setItem('timer', sec);
        $("div#timer").html(timer);
    } else {
        timer--;
        localStorage.setItem('timer', timer);
        $("div#timer").html(timer);

        if (timermin == 0) {
            localStorage.removeItem('timer', timer);
            $("div#timer").html("00");
            $("div#countermessage").html("00");

        }
    }
};

$(function() {
    $("#start").click(function() {
        localStorage.setItem('timer', sec);

    });

    $("#start2").click(function() {
        localStorage.setItem('timermin', min);
        localStorage.setItem('timerhr', hr);
    });

    setInterval(updateTimer, 1000);

    $(window).load(function() {
        localStorage.setItem('timer', sec);
        localStorage.setItem('timermin', min);
        localStorage.setItem('timerhr', hr);
    });
});

0 个答案:

没有答案