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

197
Views
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 answers
Answer question

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 Report
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!