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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4079115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:19:57  来源:igfitidea点击:

Can any desktop browsers detect when the computer resumes from sleep?

javascriptsleep

提问by Zak Linder

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 中可用,那就太好了。有谁知道这样的事情是否实施?

回答by andrewmu

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);

回答by Lynn Neir

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 执行,这可能会导致错误的唤醒指示。解决此问题的一种方法是使用网络工作者(在较新的浏览器上受支持)。...

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);

回答by Paul Okopny

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.

希望这会在将来对某人有所帮助。

回答by brightDot

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

除了其他人的非常好的答案和解释外,您还可以依赖online,offline事件。撇开是否online真的在线不谈,通常,除了真正断开互联网连接之外,当用户的机器从睡眠中恢复时,也会触发此事件。

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

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

回答by Serhii Lyzun

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

setInterval(function() {
  var currentTime = (new Date()).getTime();
  if (currentTime > (lastTime + 2000*2)) {  // ignore small delays
    setTimeout(function() {
     //enter code here, it will run after wake up
    }, 2000);
  }
  lastTime = currentTime;
}, 2000);