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

216
Visualizações
Javascript SetInterval execution delay in while switch browser tab

As part of my react app, I have some setTimeout method to run timer and if that reached zero there is prompt will be pops up, which is working fine as long as the browser in in active state, suppose when we switch between one browser tab to another, I had a issue in delay of execution of this setTimeout intervals, it takes more time to reach zero than the configured one or the initial value that we setting.

Let's say timeoutInterval is 120 seconds as default value, then if we switch another tab this setInterval execution latency issue occured. any solution for this ?

Below is my setInterval method to execute timer.

 const [inactiveTimer, setInactiveTimer] = useState(120);
 useEffect(() => {    
     const inactiveInterval = setInterval(() => {
         if (timeoutInterval > 0) {
             setInactiveTimer(timeoutInterval - 1);
         } else if (timeoutInterval === 0) {
             setShowModal(true);
         }
     }, 1000);
     return () => {
        clearInterval(inactiveInterval);
     };
 }, []);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When a tab is not active, the function can be called at a maximum of one per second. One option to solve this is to use Web Workers, because they run in separate process, hence are not slowed down.

Another option to solve this issue you can a hack by playing an ~empty sound which will force the browser to keep the regular performance.

More about this inactivation issue - https://codereview.chromium.org/6577021

Empty sound hack - How to make JavaScript run at normal speed in Chrome even when tab is not active?

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