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

198
Visualizações
Setinterval is stop working when tab is idle or working on another tab,

I have created a solution for sessiontimeout popup, to check idle time I am using a setInterval as mentioned below

    // Increment the idle time counter every 1 second.
    idleInterval = setInterval(timerIncrement, 2000, (@warningShowDelayMinutes * 60), (@sessionTimeout * 60), @warningTimeLeft);

function timerIncrement(warningShowDelay, sessionTimeout) {
        idleTime++;
        console.log("before dialog open idleTime - :" + idleTime);
        if (idleTime >= warningShowDelay && idleTime <= sessionTimeout) {
            var isWarningPopupOpened = $('#session-expire-warning').dialog('isOpen');
            if (!isWarningPopupOpened) {
                $("#session-expire-warning").dialog("open");
                //console.log("before dialog after idleTime -:" + idleTime);
            }
        }
    }

but when we move to another tab/ another window, setInterval stops working, I also tried with requestAnimationFrame but it also stop working after we move to another tab or window as mentioned below.

var last = 0;
function timerIncrement(now) {
     if (!last || now - last >= 1000) {
        last = now;
        //createNewObject();
   
        idleTime++;
        console.log("before dialog open idleTime - :" + idleTime);
        if (idleTime >= (@warningShowDelayMinutes * 60) && idleTime <=  (@sessionTimeout * 60)) {
            var isWarningPopupOpened = $('#session-expire-warning').dialog('isOpen');
            if (!isWarningPopupOpened) {
                $("#session-expire-warning").dialog("open");
                console.log("before dialog after idleTime -:" + idleTime);
            }
        }
        }
        requestAnimationFrame(timerIncrement);
    }
    requestAnimationFrame(timerIncrement);

can anyone help on this to continue the setinetrval or requestAnimationFrame after tab switching ? I am new to javascripts

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use the worker-timers NPM package to overcome this hurdle. It uses Web worker to prevent the timer(s) being throttled by the browser.

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