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

374
Visualizações
Web based Timer application using window.setInterval method in JavaScript does not work as expected on mobile browser (Chrome, Firefox etc.)

Web-based timer application using window.setInterval method in JavaScript does not work as expected in the mobile browser. The reason behind this is that after a certain time, the display goes off due to user inactivity and an application is moved to the background for power saving. Due to which window.setInterval does not trigger periodically and the entire calculation goes for a toss.

However, I have noticed that certain web-based timer works fine in the mobile browser despite these limitations. E.g. Google Timer. Despite the above limitation Google Timer works fine even in offline mode. What techniques are used by these applications so they work on mobile browser as well.

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

0

You need to use system time in the calculations. That way when your page comes forth again it’ll show status based on current time instead of relying on past steps.

btn.addEventListener('click', () => {
  btn.disabled = true;

  const start = new Date().getTime();
  const end = input.value * 60 * 1000 + start;

  let timer = setInterval(update, 1000);

  update();

  function update() {
    const current = new Date().getTime();
    let remaining = (end - current) / 1000;
    
    if (remaining <= 0) {
      clearInterval(timer);
      btn.disabled = false;
      output.textContent = 'Done!'
    } else {
      output.textContent = `${Math.floor(remaining / 60)}m ${Math.floor(remaining%60)}s`;
    }
  }
  
});
<button id="btn">Countdown</button>
<input id="input" type="number" value="2" min=1 max=59 size=5 />mins
<p id="output">0m 0s</p>

over 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
Assistente virtual