浏览器暂停/挂起检测 [英] Browser suspend / hang detection

查看:1204
本文介绍了浏览器暂停/挂起检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检测到浏览器被暂停/冻结/挂起(例如:通过操作系统锁定屏幕)?

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

我遇到了ajax计时器机制的问题。单击按钮会向启动时钟的控制器发送ajax请求。在此期间浏览器打开,另一个脚本(通过setInterval)使时钟运行。当再次单击该按钮时,ajax请求将停止计时器并且setInterval被终止。

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.

但是,我需要检测,该浏览器已冻结(javascript已停止),如果是,发一个ajax请求来更新计时器。

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

我想知道它是否真的可以通过javascript和现代浏览器实现。是吗?

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

编辑:
我想我第一次没有解释清楚。
无限循环是必须的。这些元素是计时器(秒表)。而且我不想检测脚本是否因为js bug或浏览器重载而冻结。

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.

我想检测浏览器(js)是否因为浏览器停止了OS锁定屏幕,os睡眠或其他内容。

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

推荐答案

不保证跨浏览器兼容性或任何标准合规性;它有点hackish,取决于环境,真的。

No guarantees of cross-browser compatibility or any sort of standards compliance; 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.

jsFiddle here ,公平警告,除非您真的想要开始无限​​循环,否则不要点击按钮。

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 combat 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 computer some breathing room.

这篇关于浏览器暂停/挂起检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆