Home > Software design >  How does Chrome (and other browsers) handle a “setTimeout” set to higher than 1000 milliseconds when
How does Chrome (and other browsers) handle a “setTimeout” set to higher than 1000 milliseconds when

Time:04-20

I’m debugging some login/logout issues on a site maintained by an outside vendor for a client and hopefully the title explains things, but more details are below.

As I understand it, any JavaScript running in a hidden tab in Chrome will alter the value of any setTimeout to be 1000 milliseconds to maintain overall system performance and not waste resources. As explained on this site:

“Browsers do exactly this. For inactive tabs, they automatically throttle timers to run every 1 second, regardless of the original delay specified in the code. For example, if the code originally used setInterval() to run some code every 50 ms, once the application is moved to a background tab, the interval automatically becomes 1000 ms (1 second).”

That definitely makes sense in the case of a setTimeout value that is set lower than 1,000 milliseconds like 50 milliseconds and such. But what about values higher than 1,000 milliseconds?

Will Chrome set the setTimeout value to 1,000 milliseconds even though 900,000 milliseconds (aka: 900 seconds, aka: 15 minutes) is clearly higher than that?

Here is a chunk of code from the vendor’s web page I believe is causing issues I am attempting to assist in debugging. It doesn’t do its job when it is in a hidden tab.

function keepSessionAlive() {
    if (externalAdminWindow && externalAdminWindow.closed) {
        externalAdminWindow = null;
    }
    if (externalFGWindow && externalFGWindow.closed) {
        externalFGWindow = null;
    }
    if (externalAdminWindow || externalFGWindow) {
        $find("ctl00_RadAjaxManager1").ajaxRequest("KeepAlive");
        setTimeout(function() {
            keepSessionAlive();
        }, 900000);
    }
}

CodePudding user response:

Most browsers throttle timeouts in inactive tabs. It means that all setTimeout's will have a minimum of 1000ms timeout. Everything longer than that should work as expected. So, answering your question, 15min timeout should not be affected.

Check out setTimeout's Timeouts in inactive tabs section on MDN: https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#timeouts_in_inactive_tabs

  • Related