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

111
Vistas
My Bootstrap Modal button doesn't stop a setInterval unless it is clicked in the first seconds. How to fix this

I have a function that reloads the page every 2 seconds and it starts with the first load. I want this reload to stop as soon as I click the Modal Bootstrap 4.7 button. My code works ok if I click within the first 2 seconds interval. Otherwise it no longer obeys my call. why?

var myTimer = null;

function startTimer() {
  myTimer = setInterval(function () {
    somefunction();
  }, 2000);
}

function stopTimer() {
  clearInterval(myTimer);
  myTimer =null;
}

document.addEventListener("DOMContentLoaded", function() {
  startTimer();
});

$('#idModalButton').click(function(){
      stopTimer();
      $('#idModal').modal('show');
    });

I also tried the following without success..same problem..only works in the first 2 seconds.

const myInterval = setInterval(myTimer, 2000);
    function myTimer() {
      somefunction();
    }
    

    $(document).on("click", "#idButtonModal", function(){ 
      clearInterval(myInterval);
      $('#idModal').modal('show');
    });
about 4 years ago · Juan Pablo Isaza
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