当PC从睡眠模式唤醒时开始调用js功能 [英] start calling js function when PC wakeup from sleep mode

查看:234
本文介绍了当PC从睡眠模式唤醒时开始调用js功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的aspx页面中,我使用xmlhttp.response来更新此页面的一部分。此更新每3秒发生一次,使用js函数。但是当我的电脑进入睡眠模式时,此更新不会发生。还行吧。但是当PC从睡眠模式中唤醒时,我需要自动启动更新而不必重新加载此页面。这个怎么做?

In my aspx page, i use xmlhttp.response to update a part of this page. This update occurs in every 3 seconds using js function. But when my PC goes to sleep mode, this update doesn't happen. This is OK. But when PC wake up from sleep mode, i need to start update automatically without reload this page. How to do this?

推荐答案

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

You can detect disruptions in the JS timeline (e.g. laptop sleep, alert windows that block JS excecution, debugger statements that open the debugger) by comparing change in wall time to 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();

这篇关于当PC从睡眠模式唤醒时开始调用js功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆