Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
How to stop a stopwatch when it reaches 0

I am new to jquery so I might be making some. obvious mistake here, but the if statement at the bottom is not running.
I am making a timer, and I want the stopwatch to stop when it reaches 0, although I don't know how to do that either.

var i = 60;
 
$('button[id=skill]').click(function (e){
$(this).hide(1000)
    setInterval(function () {
        $("#stopWatch").html(i);
        i--;
    }, 1000);
});

$("#resetButton").click(function (e) {
    i = 60;return false;
 
});
if(i==0)
{
  i=20
 //I also want the stopwatch to stop when it reaches 0, don't know how to do that though.
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to save the interval to a variable when you create it. Then you can clear the timer when you are done.

var i = 60;
var timer = null;
 
$('#skill').click(function() {
    $(this).hide(1000);
    timer = setInterval(function() {
        $("#stopWatch").html(i);
        i--;
        if (i===0) {
          clearInterval(timer);
        }
    }, 1000);
});

$("#resetButton").click(function() {
    i = 60;
    return false;
});

EDIT: I've fixed a logic error since my original post. The check for i==0 is now inside the timer where it can actually run. I just copied it from OP's original code without looking close enough. Fixed now!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!