jquery - Reset javascript function - Stack Overflow

I am having trouble getting a javascript function to reset itself after an onclick event. When I click

I am having trouble getting a javascript function to reset itself after an onclick event. When I click the "Start" button the counter begins to count up. But when I click the "Reset" button nothing happens. I need the timer to reset to "0:00" and wait for me to click "Start" again. Here is my code:

<script type="text/javascript">
var seconds = 0;
var minutes = 0;

function zeroPad(time) {
    var numZeropad = time + '';
    while(numZeropad.length < 2) {
        numZeropad = "0" + numZeropad;
    }
    return numZeropad;
}

function countSecs() {
    seconds++;

    if (seconds > 59) {
         minutes++;
         seconds = 0;
    }
    document.getElementById("timeBox").innerHTML = "Time " + zeroPad(minutes) + ":" + zeroPad(seconds);
}

 function startTimer() {
     action = window.setInterval(countSecs,1000);
 }


function resetTimer() {
    var seconds = 0;
    var minutes = 0;
 }

</script>

<body>
<button onclick = "startTimer()">Start</button>
<div id="timeBox">Time 00:00</div>

<button onclick = "resetTimer">Reset</button>
</body>

I am having trouble getting a javascript function to reset itself after an onclick event. When I click the "Start" button the counter begins to count up. But when I click the "Reset" button nothing happens. I need the timer to reset to "0:00" and wait for me to click "Start" again. Here is my code:

<script type="text/javascript">
var seconds = 0;
var minutes = 0;

function zeroPad(time) {
    var numZeropad = time + '';
    while(numZeropad.length < 2) {
        numZeropad = "0" + numZeropad;
    }
    return numZeropad;
}

function countSecs() {
    seconds++;

    if (seconds > 59) {
         minutes++;
         seconds = 0;
    }
    document.getElementById("timeBox").innerHTML = "Time " + zeroPad(minutes) + ":" + zeroPad(seconds);
}

 function startTimer() {
     action = window.setInterval(countSecs,1000);
 }


function resetTimer() {
    var seconds = 0;
    var minutes = 0;
 }

</script>

<body>
<button onclick = "startTimer()">Start</button>
<div id="timeBox">Time 00:00</div>

<button onclick = "resetTimer">Reset</button>
</body>
Share Improve this question asked Jul 2, 2012 at 13:55 three3three3 2,84614 gold badges58 silver badges89 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 1

Call the clearInterval() method.

function resetTimer() {
   window.clearInterval(action);
 }

This is a scoping issue, using var inside a function, makes seconds and minutes local to that function. Removing the leading var will start you off in the right direction.

function resetTimer() {
  seconds = 0;
  minutes = 0;
}

Onclick events must call functions like: onclick="resetTimer();" with the parenthesis at the end. Some browsers may try to submit on button clicks if you don't define type="button". I didn't assume you wanted reset timer to stop the timer so I added a stop button.

http://jsfiddle/iambriansreed/WRdSK/

<button type="button" onclick="startTimer();">Start</button>
<div id="timeBox">Time 00:00</div>

<button type="button" onclick="resetTimer();">Reset</button>
<button type="button" onclick="stopTimer();">Stop</button>

<script>

    window.seconds = 0;
    window.minutes = 0;

    function startTimer() {
        window.action = setInterval(countSecs,1000);
    }
    function resetTimer() {
        seconds = 0;
        minutes = 0;
    }
    function stopTimer() {
        clearInterval(action);        
        seconds = -1;
        minutes = 0;
        countSecs();
    }
    function zeroPad(time) {
        var numZeropad = time + '';
        while(numZeropad.length < 2) {
            numZeropad = "0" + numZeropad;
        }
        return numZeropad;
    }

    function countSecs() {
        seconds++;

        if (seconds > 59) {
             minutes++;
             seconds = 0;
        }
        document.getElementById("timeBox").innerHTML = "Time " + zeroPad(minutes) + ":" + zeroPad(seconds);
    }

</script>

You have two errors in your code:

First, in the button you missed the () after the function's name in order to make an actual call:

<button onclick = "resetTimer()">Reset</button>

Second, you did not stop the interval using window.clearInterval() (MDN docu), so the timer went on and on.

// just to make it an explicit global variable. already was an implicit one.
var action;

// rest of your code
function resetTimer() {
    // clear the timer
    window.clearInterval( action );
    // reset variables
    var seconds = 0;
    var minutes = 0;
    // update output
    document.getElementById("timeBox").innerHTML = "Time " + zeroPad(minutes) + ":" + zeroPad(seconds);
 }

I set up a working fiddle here.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742296688a4417267.html

相关推荐

  • jquery - Reset javascript function - Stack Overflow

    I am having trouble getting a javascript function to reset itself after an onclick event. When I click

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信