Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda