Start calling js function when exiting computer standby

On my aspx page, I use xmlhttp.response to update part of this page. This update occurs every 3 seconds using the js function. But when my computer goes into sleep mode, this update does not occur. Good. But when the PC wakes up from sleep mode, I need to automatically start updating without reloading this page. How to do it?

+1
source share
1 answer

You can detect failures in the JS timeline (for example, a sleep laptop, warning windows that block JS excecution, statements debuggerthat open the debugger) by comparing the time changes on the wall to the expected timer delay. For example:

var SAMPLE_RATE = 3000; // 3 seconds
var lastSample = Date.now();
function sample() {
  if (Date.now() - lastSample >= SAMPLE_RATE * 2) {
    // Code here will only run if the timer is delayed by more 2X the sample rate
    // (e.g. if the laptop sleeps for more than 3-6 seconds)
  }
  lastSample = Date.now();
  setTimeout(sample, SAMPLE_RATE);
}

sample();
+4
source

Source: https://habr.com/ru/post/1533893/


All Articles