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

92
Views
Resetting the setInterval puts it in a multiple loop interval instead

In this JSfiddle, I have a code where if you click on the button, it automatically creates another set interval within the old one,

I have clearInterval but for some reason, it's not working

(Try clicking on the timer button multiple times)

<span>LIVE: Election results will refresh in <span id="time">2:00</span> minutes.</span>
<input type="button" value="Timer" id="btn">
<script>
$("#btn").on('click', function() {
  var twoMinutes = 60 * 2,
    display = document.querySelector('#time');

  startTimer(twoMinutes, display);
});

function startTimer(duration, display) {
  var timer = duration,
    minutes, seconds;
  var interval;
  clearInterval(interval);
  interval = setInterval(function() {
    minutes = parseInt(timer / 60, 10)
    seconds = parseInt(timer % 60, 10);

    minutes = minutes < 10 ? +minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;
    if (--timer < 0) {
      timer = duration;
      LoadCandidatesCharts(true);
    }
  }, 1000);

} </script>

enter link description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Problem is because you're calling the interval var within the function scope, while you should be declaring it on a global scope, try this

var interval; // global var
function startTimer(duration, display) {
  var timer = duration,
    minutes, seconds;
  
  clearInterval(interval);
  interval = setInterval(function() {
    minutes = parseInt(timer / 60, 10)
    seconds = parseInt(timer % 60, 10);

    minutes = minutes < 10 ? +minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;
    if (--timer < 0) {
      timer = duration;
      LoadCandidatesCharts(true);
    }
  }, 1000);

}
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!