任何桌面浏览器都能检测到计算机何时从睡眠状态恢复?

时间:2022-01-13 18:14:03

It would be nice if the computer's 'wake up' event was propagated to the browser and available in the JavaScript API. Does anyone know if anything like this is implemented?

如果计算机的“唤醒”事件传播到浏览器并在JavaScript API中可用,那就太好了。有谁知道这样的事情是否实施了?

4 个解决方案

#1


55  

I don't know of any direct method to do this, but one way you could get a good idea of when it happens is to set up a setInterval task that runs, say every 2 seconds, and stores the time it last ran. Then check to see if the last time it ran is very much older than 2 seconds.

我不知道有什么直接的方法可以做到这一点,但是你可以很好地了解它何时发生的一种方法是设置一个运行的setInterval任务,比如每2秒运行一次,并存储它上次运行的时间。然后检查它上次运行的时间是否超过2秒。

var lastTime = (new Date()).getTime();

setInterval(function() {
  var currentTime = (new Date()).getTime();
  if (currentTime > (lastTime + 2000*2)) {  // ignore small delays
    // Probably just woke up!
  }
  lastTime = currentTime;
}, 2000);

#2


18  

One of the problems you might encounter with methods above is that alert boxes or other modal type windows will pause JS execution possibly causing a false wake up indication. One way to solve this problem is to use web workers (supported on newer browsers)....

上面的方法可能遇到的问题之一是警报框或其他模态类型窗口将暂停JS执行可能导致错误的唤醒指示。解决此问题的一种方法是使用Web worker(在较新的浏览器上支持)....

var myWorker = new Worker("DetectWakeup.js");
myWorker.onmessage = function (ev) {
  if (ev && ev.data === 'wakeup') {
     // wakeup here
  }
}

// DetectWakeup.js (put in a separate file)
var lastTime = (new Date()).getTime();
var checkInterval = 10000;

setInterval(function () {
    var currentTime = (new Date()).getTime();

    if (currentTime > (lastTime + checkInterval * 2)) {  // ignore small delays
        postMessage("wakeup");
    }

    lastTime = currentTime;
}, checkInterval);

#3


3  

This is a little outdated, but based on the answer by Andrew Mu I've created a simple JQuery plugin to do that: https://bitbucket.org/paul.okopny/jquery.wakeup-plugin/wiki/Home

这有点过时了,但根据Andrew Mu的回答,我创建了一个简单的JQuery插件来做到这一点:https://bitbucket.org/paul.okopny/jquery.wakeup-plugin/wiki/Home

Usage is simple:

用法很简单:

$.wakeUp(function(sleep_time) {
    alert("I have slept for " + sleep_time/1000 + " seconds")
});

Hope this will help someone in the future.

希望这将有助于未来的人。

#4


0  

Apart from very good answers and explanations by others, you can also depend on online, offline events. Keeping aside whether online is really online or not, usually, this event ALSO gets triggered when user's machine is back from sleep apart from having real internet disconnection.

除了其他人的非常好的答案和解释,您还可以依赖在线,离线活动。不管在线是否真的在线,通常情况下,当用户的机器从睡眠状态恢复而不是真正的互联网断开时,此事件也会被触发。

So, the ideal solution would be having timer check combined with the online and offline events.

因此,理想的解决方案是将计时器检查与在线和离线事件相结合。

#1


55  

I don't know of any direct method to do this, but one way you could get a good idea of when it happens is to set up a setInterval task that runs, say every 2 seconds, and stores the time it last ran. Then check to see if the last time it ran is very much older than 2 seconds.

我不知道有什么直接的方法可以做到这一点,但是你可以很好地了解它何时发生的一种方法是设置一个运行的setInterval任务,比如每2秒运行一次,并存储它上次运行的时间。然后检查它上次运行的时间是否超过2秒。

var lastTime = (new Date()).getTime();

setInterval(function() {
  var currentTime = (new Date()).getTime();
  if (currentTime > (lastTime + 2000*2)) {  // ignore small delays
    // Probably just woke up!
  }
  lastTime = currentTime;
}, 2000);

#2


18  

One of the problems you might encounter with methods above is that alert boxes or other modal type windows will pause JS execution possibly causing a false wake up indication. One way to solve this problem is to use web workers (supported on newer browsers)....

上面的方法可能遇到的问题之一是警报框或其他模态类型窗口将暂停JS执行可能导致错误的唤醒指示。解决此问题的一种方法是使用Web worker(在较新的浏览器上支持)....

var myWorker = new Worker("DetectWakeup.js");
myWorker.onmessage = function (ev) {
  if (ev && ev.data === 'wakeup') {
     // wakeup here
  }
}

// DetectWakeup.js (put in a separate file)
var lastTime = (new Date()).getTime();
var checkInterval = 10000;

setInterval(function () {
    var currentTime = (new Date()).getTime();

    if (currentTime > (lastTime + checkInterval * 2)) {  // ignore small delays
        postMessage("wakeup");
    }

    lastTime = currentTime;
}, checkInterval);

#3


3  

This is a little outdated, but based on the answer by Andrew Mu I've created a simple JQuery plugin to do that: https://bitbucket.org/paul.okopny/jquery.wakeup-plugin/wiki/Home

这有点过时了,但根据Andrew Mu的回答,我创建了一个简单的JQuery插件来做到这一点:https://bitbucket.org/paul.okopny/jquery.wakeup-plugin/wiki/Home

Usage is simple:

用法很简单:

$.wakeUp(function(sleep_time) {
    alert("I have slept for " + sleep_time/1000 + " seconds")
});

Hope this will help someone in the future.

希望这将有助于未来的人。

#4


0  

Apart from very good answers and explanations by others, you can also depend on online, offline events. Keeping aside whether online is really online or not, usually, this event ALSO gets triggered when user's machine is back from sleep apart from having real internet disconnection.

除了其他人的非常好的答案和解释,您还可以依赖在线,离线活动。不管在线是否真的在线,通常情况下,当用户的机器从睡眠状态恢复而不是真正的互联网断开时,此事件也会被触发。

So, the ideal solution would be having timer check combined with the online and offline events.

因此,理想的解决方案是将计时器检查与在线和离线事件相结合。