最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Browser suspendhang detection - Stack Overflow

programmeradmin2浏览0评论

Is there any way to detect that browser was suspended / freezed / hanged (for example: by OS lock screen)?

I have an issue with ajax timer mechanism. Button on click sends ajax request to controller that start the clock. In the meantime the browser is on, another script (by "setInterval") make the clock running. When the button is clicked again, ajax request stops the timer and setInterval is killed.

However, I need to detect, that browser was freezed (javascript stopped) and if so, make an ajax request to update the timer.

I was wondering if it's actually possible by javascript and modern browsers. Is it?

EDIT: I think I didn't explain it well at the first time. Infinite loop is must-be. Those elements are timers (stopwatchs). And I don't want to detect if the script is freeze because of the js bug or browser overload.

I want to detect if the browser (js) was stopped because of OS lock screen, os sleep or something else.

Is there any way to detect that browser was suspended / freezed / hanged (for example: by OS lock screen)?

I have an issue with ajax timer mechanism. Button on click sends ajax request to controller that start the clock. In the meantime the browser is on, another script (by "setInterval") make the clock running. When the button is clicked again, ajax request stops the timer and setInterval is killed.

However, I need to detect, that browser was freezed (javascript stopped) and if so, make an ajax request to update the timer.

I was wondering if it's actually possible by javascript and modern browsers. Is it?

EDIT: I think I didn't explain it well at the first time. Infinite loop is must-be. Those elements are timers (stopwatchs). And I don't want to detect if the script is freeze because of the js bug or browser overload.

I want to detect if the browser (js) was stopped because of OS lock screen, os sleep or something else.

Share Improve this question edited Jan 7, 2013 at 10:11 sznowicki asked Jan 7, 2013 at 9:05 sznowickisznowicki 1,4015 gold badges18 silver badges33 bronze badges 6
  • The problem with a browser page hanging is that it's often a JavaScript infinite loop or something like that causing the hang. You can't run JS when that happens, since it'll wait for the hanging JS to plete. – Cerbrus Commented Jan 7, 2013 at 9:07
  • 3 i think it will be better if you dont make browser freeze with infinite loop . – Arun Killu Commented Jan 7, 2013 at 9:08
  • How would you make the ajax request if the browser is frozen? – JJJ Commented Jan 7, 2013 at 9:08
  • 1 did you reaally think that when browser is ffrozen there's a little corner of it that allows you to run code ? sounds like a car that runs out of gas but you want to drive it some more – charlietfl Commented Jan 7, 2013 at 9:14
  • Juhana: you didn't understand or I didn't describe it well. I don't want to make a call while browser is on sleep. I want to detect, that browser was asleep and then make a call (when it resume working). – sznowicki Commented Jan 7, 2013 at 9:56
 |  Show 1 more ment

4 Answers 4

Reset to default 3

No guarantees of cross-browser patibility or any sort of standards pliance; it's a little hackish and depends upon circumstance, really.

(function() {
    // milliseconds
    var lastTime = (new Date).getTime()
    ,   acceptableDelta = 500
    ,   tick = 1000
    ,   hung = false;

    function hangman() {
        var now = (new Date).getTime();
        if(now - lastTime > (tick + acceptableDelta)) {
            hung = true;
        } else if(hung) {
            hung = false;
            console.warn('Possible browser hangup detected.');
        }
        lastTime = now;
    }

    setInterval(hangman, tick);
}());

The concept behind this is that if the browser is capable of stopping a runaway script for the user but leaving the rest of the page somehow intact, the timer will detect this discrepancy and warn the user in the console.

Demonstration on jsFiddle here, fair warning, please do not click the button unless you actually want an infinite loop to begin.


In the end, you might want to rethink why you're doing this in the first place and bat any sources for a potential browser hangup at the design level. Avoid heavy and sequential workloads without pause, infinite loops, processor-intensive operations, and such. Space out your tasks with asynchronous callbacks and place strategic delays in your code to allow the browser and the user's puter some breathing room.

Not possible. The only thing remotely close to that would be to listen for the window close and executing some last bit of code in your codes dying breath.

window.onbeforeunload = function(event) {
    //do something, ask if the user really wants to exit the page, cancel ajax or whatever it is you need to achieve.
}

For certain cases, this is now possible via the Page Lifecycle API (Chrome developer documentation)

It won't determine the OS or browser specific reason for stopping the page, but will allow for detection and handling.

From the New Features Added in Chrome 68 section:

The previous chart shows two states that are system-initiated rather than user-initiated: frozen and discarded. As mentioned previously, browsers today already occasionally freeze and discard hidden tabs (at their discretion), but developers have no way of knowing when this is happening.

In Chrome 68, developers can now observe when a hidden tab is frozen and unfrozen by listening for the freeze and resume events on document.

document.addEventListener('freeze', (event) => {
  // The page is now frozen.
});

document.addEventListener('resume', (event) => {
  // The page has been unfrozen.
});

From Chrome 68 the document object now includes a wasDiscarded property on desktop Chrome (Android support is being tracked in this issue). To determine whether a page was discarded while in a hidden tab, you can inspect the value of this property at page load time (note: discarded pages must be reloaded to use again).

if (document.wasDiscarded) {
  // Page was previously discarded by the browser while in a hidden tab.
}

For advice on what things are important to do in the freeze and resume events, as well as how to handle and prepare for pages being discarded, see developer remendations for each state.

Ajax request is done with javascript code. If any javascript caused the browser to hang/freeze, it means that there might be a bug which does not allow to move ahead of that particular javascript code. Once it freeze the browser, the BROWSER tries to attempt to detect the bug (not the script since the script itself is freezed). Its in the software (browser software be it IE/FF/chrome).

The best would be to wrap your javascript code in

try
{
}
catch
{
}

To capture the errors.

发布评论

评论列表(0)

  1. 暂无评论