当PC从睡眠模式唤醒时,开始调用js函数

Start calling js function when PC wakeup from sleep mode

本文关键字:开始 调用 js 函数 唤醒 PC 模式      更新时间:2023-09-26

在我的aspx页面中,我使用xmlhttp.response来更新这个页面的一部分。使用js函数每3秒更新一次。但是当我的电脑进入睡眠模式时,这个更新就不会发生了。这没问题。但是当电脑从睡眠模式唤醒时,我需要在不重新加载这个页面的情况下自动开始更新。如何做到这一点?

你可以通过比较墙时间的变化和预期的计时器延迟来检测JS时间线中的中断(例如笔记本电脑睡眠,阻止JS执行的警报窗口,打开调试器的debugger语句)。例如:

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