Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
Javascript Countdown Timer w/ Bootstrap Modal Pause

See my code below, I am trying to get the countdown timer to pause when a bs.modal is shown and to resume again once the modal is hidden. This works perfectly if the modal is opened and then closed but if you try to reopen the modal again, the timer just continues. I am probably missing something obvious but am not the best when it comes to JS.

(function countdown(remaining) {
    if(remaining === 0) {
        location.reload(true);
    }
    document.getElementById('countdown').innerHTML = remaining;

    $(document).on('shown.bs.modal', function(e) {
        console.log("modal triggered");
        clearTimeout(timeout);
    });

    $(document).on('hide.bs.modal', function(e) {
        console.log("modal closed");
        
        countdown(remaining - 1);

    });

    timeout = setTimeout(function(){
        if(remaining != 0) {
            countdown(remaining - 1);
        }
    }, 1000);

})(300);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It seems the issue with the existing code is a timing problem that results in multiple timeouts getting set simultaneously, some of which can then never be cleared because their id's get lost. It's easy to get into such a situation when you're setting up a new timeout for every iteration. So, as I mentioned, setInterval() is a better choice here. Below is a solution using setInterval(). Also note that it's written so as to ensure that any existing interval is cleared before its id is overwritten (in the function clearAndSetInterval.)

    (function (remaining) {
        var interval;
        var clearAndSetInterval = function () {
            clearInterval(interval);
            interval = setInterval(function (){
                if (remaining <= 0) {
                    clearInterval(interval);
                    location.reload(true);
                }

                document.getElementById('countdown').innerHTML = remaining;
                remaining = remaining - 1;
            }, 1000);
        };

        $(document).on('shown.bs.modal', function(e) {
            clearInterval(interval);
        });

        $(document).on('hide.bs.modal', function (e) {
            clearAndSetInterval();
        });

        clearAndSetInterval();
    })(300);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda