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


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



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?





I doubt it since it would probably have to bridge the gap between isolated browser and operating system, but I'll be interested to find out of it is, in fact, possible.
– Joel Etherton
Nov 2 '10 at 15:22





What type of device?
– Šime Vidas
Nov 2 '10 at 15:22





Define "sleep", as this term varies widely per-device.
– Nick Craver
Nov 2 '10 at 15:24





I've edited the question to be specifically about desktop browsers. I'd this question to be a bit device-agnostic since webapps that would use such an API would presumably target multiple platforms/devices.
– Zak Linder
Nov 4 '10 at 4:38





@Nick in my specific use-case the webapp initiates a constant connection with the server. If the device goes to sleep this connection is severed and should be re-initiated on wake-up. There doesn't seem to be a good way of doing this automatically (I dont want to force the user to press a button :P).
– Zak Linder
Nov 4 '10 at 4:41





4 Answers
4



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.


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





This is similar to how I would go about it -- only, use a much larger interval than two seconds :-) I imagine 5minutes would be a reasonably small value (it is rare that my computer sleeps for any small amount of time). Also, another way is to record how many "ticks" occurred vs. how many were supposed to occur (keep it on an average). There is also the edge-case of a time change (DST -- so best to use UTC -- or user) that may be marginalized.
– user166390
Nov 4 '10 at 4:46






+1 for a reasonable workaround :)
– Zak Linder
Nov 4 '10 at 4:48





It helped me :) +1
– Varun
Apr 22 '11 at 6:51





It is good, but when you fire an ajax request after the sleep, it fails! Why? Having alert statement before calling the ajax request would work out! Why?
– moderns
May 18 '14 at 19:54






A fine idea, but remember that setInterval and setTimeout execution speed can be affected when the tab is in the background, and that's by design.
– a paid nerd
Aug 26 '14 at 1:20


setInterval


setTimeout



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


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





Could you please give a demo? I have tried working it out, but it doesn't give anything. Not even in the console.
– user5799177
Sep 21 '16 at 15:20



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



Usage is simple:


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



Hope this will help someone in the future.



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.


online


offline


online



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages