Search code examples
javascripthtmlcountdownstopwatch

I want to make a Countdown Stopwatch


I want to make a countdown Stop watch in which I can define time and it reduce to 0:00 in which I am having 3 button i.e Start, Stop, Reset.

Can anyone help I Searched many website's but did't get the thing I want.


Solution

  • try this hope work for you

    var CCOUNT;
    $(document).ready(function () {
        $('#btnct').click(function () {
            CCOUNT = $('#seconds').val();
            cdreset();
        });
    });
    var t, count;
    
    function cddisplay() {
        document.getElementById('timespan').innerHTML = count;
    }
    
    function countdown() {
        // starts countdown
        cddisplay();
        if (count === 0) {
            // time is up
        } else {
            count--;
            t = setTimeout(countdown, 1000);
        }
    }
    
    function cdpause() {
        // pauses countdown
        clearTimeout(t);
    }
    
    function cdreset() {
        // resets countdown
        cdpause();
        count = CCOUNT;
        cddisplay();
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    
    <form id="frm">Seconds:
        <input type="text" id="seconds" name="seconds" value="0" size="2" maxlength="2" />
        <br/>
        <input type="button" id="btnct" value="Input" />
    </form>
    <span id="timespan"></span>
    
    <input type="button" value="Start" onclick="countdown()">
    <input type="button" value="Stop" onclick="cdpause()">
    <input type="button" value="Reset" onclick="cdreset()">