简体   繁体   中英

JavaScript countdown timer with cookies stops after one minute

I have this countdown timer. For some reason it stops counting after one minute - it just stops. If I count for 55 minutes it stops at 54:00 If I count for 2 minutes it stops at 1:00 Any ideas how do I fix that so it continues up to zero?

Here is the JSFiddle link: Countdown timer with cookies

And the JS code:

function countdown(minutes) {
var seconds = 60;
var mins = minutes;

if(getCookie("minutes")&&getCookie("seconds"))
{
     var seconds = getCookie("seconds");
     var mins = getCookie("minutes");
}

function tick() {

    var counter = document.getElementById("timer");
    setCookie("minutes",mins,10)
    setCookie("seconds",seconds,10)
    var current_minutes = mins-1
    seconds--;
    counter.innerHTML = 
    current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
    //save the time in cookie



    if( seconds > 0 ) {
        setTimeout(tick, 1000);
    } else {

            if(mins > 1){

               // countdown(mins-1);   never reach “00″ issue solved:Contributed by Victor Streithorst    
               setTimeout(function () { countdown(mins - 1); }, 1000);

            }
        }
    }
    tick();
}
function setCookie(cname,cvalue,exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname+"="+cvalue+"; "+expires;
}
 function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
countdown(55);

I don't know why you are using the cookies here but, putting that aside, your problem seems to be the following lines:

var seconds = getCookie("seconds");
var mins = getCookie("minutes");

In your code after you finished the first minute you call the countdown function again with a minute less. However, the lines above returns the minute variable back to its previous value. So you end up stuck in the same minute after one minute pass.

So, I assume, you need to get the time from the cookies only once (first time the countdown runs). You can use something like this:

var firstTime = true;

function countdown(minutes) {
    var seconds = 60;
    var mins = minutes;

    if(firstTime && getCookie("minutes")&&getCookie("seconds"))
    {
        firstTime = false;
        seconds = getCookie("seconds");
        mins = getCookie("minutes");
    }
    ...

Note that var is unnecessary in the if clause since you already defined seconds and mins variables.

Code with day:hours:minute:second support. Just add number of minutes into timer span. For one day test, I entered 1440 minutes.

<html>
<body>
<span id="countdown" class="timer">1440</span>
<script type="text/javascript">

var first_time = true;
var countdownTimer;
var seconds = document.getElementById('countdown').innerHTML * 60;
//alert(seconds);
if(!isNaN(seconds) && seconds > 0 ) {

    function timer() {

        if(first_time) {
            countdownTimer = setInterval('timer()', 1000);
            first_time = false;
        }

        var days        = Math.floor(seconds/24/60/60);
        var hoursLeft   = Math.floor((seconds) - (days*86400));
        var hours       = Math.floor(hoursLeft/3600);
        var minutesLeft = Math.floor((hoursLeft) - (hours*3600));
        var minutes     = Math.floor(minutesLeft/60);
        var remainingSeconds = seconds % 60;
        if (remainingSeconds < 10) {
            remainingSeconds = "0" + remainingSeconds; 
        }
        document.getElementById('countdown').innerHTML = (days < 10 ? "0" : "") + days + ":" + (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":" + (remainingSeconds < 10 ? "0" : "") + remainingSeconds;
        if (seconds == 0) {
            clearInterval(countdownTimer);
            document.getElementById('countdown').innerHTML = "Completed";
        } else {
            seconds--;
        }
    }

    timer()

    //var countdownTimer = setInterval('timer()', 1000);
}
</script>

</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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