問題描述
在我的 aspx 頁面中,我使用 xmlhttp.response
來更新此頁面的一部分.使用 js 函數,此更新每 3 秒發生一次.但是當我的電腦進入睡眠模式時,這個更新不會發生.還行吧.但是當電腦從睡眠模式喚醒時,我需要自動開始更新而不重新加載這個頁面.如何做到這一點?
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 執行的警報窗口、打開調試器的 debugger
語句) 通過比較墻上時間的變化與預期的計時器延遲.例如:
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函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!