Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8176113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:02:30+00:00 2026-06-06T23:02:30+00:00

I am having trouble getting a javascript function to reset itself after an onclick

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:02:31+00:00Added an answer on June 6, 2026 at 11:02 pm

    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.net/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>
    

    ​

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having trouble getting success from this... <!-- Ajax --> <script type=text/javascript> $('a.clickup').click(function (event) {
I'm having trouble getting some javascript and Jquery to delay the appropriate amount of
I'm having a little trouble getting my head around a Javascript animated scroll issue.
I am having trouble getting the new .on click to work using 1.7. For
I'm having trouble getting C# and JavaScript/jQuery to play nice here. I have a
I'm having trouble getting an ASP button to call a jquery script, in this
I'm having some trouble getting JSON to store some Javascript as a string value.
I'm having trouble resizing an iframe after injecting it on load using javascript. I
I'm trying to learn Javascript closures. I'm having trouble getting my head around the
I'm having trouble getting click events from list items. In this page: http://bec-systems.com/list-click.html The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.