0

I am basically want the page to take a user input of the time in seconds.

After that I want the countdown to start when the user presses "Start" button "Pause" when the pause button is pressed. Also a reset button so that the user can start the countdown from the beginning.

Here is what i got so far:

<script>
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>
<body>
 <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()">
</body>`

But its not working, It worked when i left out the user input part and just initialized CCOUNT. But I want the page to take user input.

1
  • Umm... do you ever close the brackets from $(document).ready? Commented Jul 29, 2014 at 14:09

1 Answer 1

3

This works: http://jsfiddle.net/robschmuecker/XZXLk/

Javascript with jQuery

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();
}

HTML:

<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()">
Sign up to request clarification or add additional context in comments.

2 Comments

Double click or triple click the start button. Might want to set a disable/hide function.
@wrxsti absolutely correct but I'll leave that as an exercise for the OP :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.