Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
How to make setInterval work on an inactive tab? javascript

I'm developing a project on the Pomodoro study method, which counts time with setInterval, and it's working.

However, when changing tabs in the browser, the time stops counting, could someone help me?

I need the counter to keep counting when I switch tabs in the browser, otherwise the project is useless.

Thanks

function pararRelogio(intervalo) {
    clearInterval(intervalo);
}

function foco() {    
    btnFocar.disabled = true;
    let segundos = 0;
    let minutos = 0;
    intervaloPomodoro = setInterval(() => {
        segundos ++;
        tempoSegundos.innerHTML = segundos;
        tempoMinutos.innerHTML = minutos;
        if (segundos >= 60) {
            segundos = 0;
            minutos += 1;
            if(minutos >= 25) { 
                segundos = 0;
                minutos = 0;
                tempo.innerHTML = 0;
                tempoMinutos.innerHTML = 0;
                let som = document.getElementById('audiotag1').play();
                pararRelogio(intervaloPomodoro);
                btnFocar.classList.add('esconderBotao');
                btnDescansar.classList.remove('esconderBotao');
                btnFocar.disabled = false;
            }
        }
    }, 10);
};

function descanso() {    
    btnDescansar.disabled = true;
    let segundos = 0;
    let minutos = 0;
    intervaloPomodoro = setInterval(() => {
        segundos ++;
        tempoSegundos.innerHTML = segundos;
        tempoMinutos.innerHTML = minutos;
        if (segundos >= 60) {
            segundos = 0;
            minutos += 1;
            if(minutos >= 5) {
                segundos = 0;
                minutos = 0;
                tempo.innerHTML = 0;
                tempoMinutos.innerHTML = 0;
                let som = document.getElementById('audiotag1').play();
                pararRelogio(intervaloPomodoro);
                btnDescansar.classList.add('esconderBotao');
                btnFocar.classList.remove('esconderBotao');
                btnDescansar.disabled = false;
            }
        }
    }, 10);
};
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!