javascript/jquery countdown timer with JSfiddle example?

前端 未结 4 1325
后悔当初
后悔当初 2021-01-21 02:50

I am building a few things and one of them is a countdown timer, the countdown will never be over an hour so all I need to do is countdown minutes and seconds.

I have it

4条回答
  •  野的像风
    2021-01-21 03:46

    setInterval returns an identity you can use later to clearInterval:

    var interval = setInterval(function() {
        /* snip */
        $('span').html(minutes + ':' + seconds);
    
        if (parseInt(minutes, 10) == 0 && parseInt(seconds, 10) == 0)
            clearInterval(interval);
    }, 1000);
    

    And, to avoid the ever-increasing minutes -- 00000001:42 -- either:

    1. change length.minutes to minutes.length in your prefix test.
    2. cast the values to Numbers when retrieving -- var minutes = parseInt(timer[0], 10); -- and just test if (minutes < 10) ....

    Taking option #2, here's an update: http://jsfiddle.net/BH8q9/

提交回复
热议问题