Search code examples
jquerycountdownchronometer

jquery...chronometer plugin


I am searching a jquery plugin where: - you can set a time countdown - set a function when countdown arrives to 0; - start/stop/pause/reset the countdown

Can anyone help me? Thanks a lot.


Solution

  • You can write one yourself without a library quite easily:

    <!-- HTML -->
    <a href="#" class="start">start</a>
    <a href="#" class="stop">stop</a>
    <a href="#" class="pause">pause</a>
    <a href="#" class="reset">reset</a>
    
    <div class="time"></div>
    

    - ​

    //Javascript
    var startValue = 120000; //Number of milliseconds
    var time = new Date(startValue);
    var interv;
    
    
    function done(){
        alert("Timer reached 00:00!");
    }
    $(function(){
        displayTime();
        $(".start").on("click", function(){
        interv = setInterval(function(){
            time = new Date(time - 1000);
            if(time<=0){
                done();
                clearInterval(interv);
            }
            displayTime();
        }, 1000);
        });
        $(".stop").on("click", function(){
            clearInterval(interv);
            time = new Date(startValue);
            displayTime();
        });
        $(".pause").on("click", function(){
            clearInterval(interv);
        });
        $(".reset").on("click", function(){
            time = new Date(startValue);
            displayTime();
        });
    });
    
    function displayTime(){
        $(".time").text(fillZeroes(time.getMinutes()) + ":" + fillZeroes(time.getSeconds()));
    }
    
    function fillZeroes(t){
        t = t+"";
        if(t.length==1)
            return "0" + t;
        else
            return t;
    }
    ​
    

    JsFiddle