Javascript计时器会在计算机从睡眠状态恢复后发出过多警报
我的网页上有数据网格,需要每隔几分钟刷新一次。当Javascript计时器过期时,使用PHP从mysql数据库查询数据。导入数据后,需要对其进行样式设置,以使问题易于看到,因此我会触发另一个计时器,该计时器将每2.5秒翻转三次,然后关闭。我提出了这个解决方案,因为数据网格没有立即填充,所以在查询之后毫不延迟地运行它会导致它丢失大部分数据 问题: 1) 最大的问题是,当电脑进入睡眠状态时,如果网页在浏览器中保持打开状态,就会出现几个空警报。考虑到有多少人会在工作中使用这个网站,这种行为是完全不可接受的。我没有主意了 2) 我希望有更好的方法在导入时处理数据,这样我就不必使用$(“td”)。刷新后,每个()都会覆盖整个页面。我使用的是dhtmlx库中的组件,所以要打开函数并在内部处理这个问题有点困难。我能做的就是对数据进行后期处理,但我愿意接受各种想法Javascript计时器会在计算机从睡眠状态恢复后发出过多警报,javascript,sleep,alert,setinterval,Javascript,Sleep,Alert,Setinterval,我的网页上有数据网格,需要每隔几分钟刷新一次。当Javascript计时器过期时,使用PHP从mysql数据库查询数据。导入数据后,需要对其进行样式设置,以使问题易于看到,因此我会触发另一个计时器,该计时器将每2.5秒翻转三次,然后关闭。我提出了这个解决方案,因为数据网格没有立即填充,所以在查询之后毫不延迟地运行它会导致它丢失大部分数据 问题: 1) 最大的问题是,当电脑进入睡眠状态时,如果网页在浏览器中保持打开状态,就会出现几个空警报。考虑到有多少人会在工作中使用这个网站,这种行为是完全不可接
变量声明和初始计时器设置:
//Set a timer to refresh all grid data every 4 minutes
var RefreshDataTimer = setInterval(timerMethod, 240000);
//Set an initial timer for the box coloring method
var RefreshBoxTimer = setInterval(boxMethod, 3000);
网格数据刷新计时器处理程序:
//Every 2.5 minutes this function will reload all of the grid data.
//It also triggers the boxMethod function.
function timerMethod() {
$("#UpdateStatus_Div").fadeIn(300).delay(1000).fadeOut(300);
RunStatusGrid.clearAndLoad("data/RunStatus.php");
CmdlineGrid.clearAndLoad ("data/CmdlineDataGet.php");
Results0Grid.clearAndLoad ("data/Results0Data.php");
Results1Grid.clearAndLoad ("data/Results1Data.php");
Results2Grid.clearAndLoad ("data/Results2Data.php");
Results3Grid.clearAndLoad ("data/Results3Data.php");
RefreshBoxTimer = setInterval(boxMethod, 2500);
}
//This function searches all of the grids for ERROR/FAIL/SKIP/WARN and changes
//the cell background colors to clearly show where there are issues.
//It will only run 3 times because it is CPU intensive. The 2.5 second delay is because
//the clearAndLoad functions are run as background tasks and don't complete immediately.
var BoxCounter = 0;
function boxMethod() {
BoxCounter++;
if(BoxCounter >= 4) {
BoxCounter = 0;
clearInterval(RefreshBoxTimer);
}
$("td").each(function(index) {
if( (this.innerHTML == "ERROR") || (this.innerHTML == "FAIL")) {
this.style.backgroundColor = "red";
this.style.fontWeight = "bold";
} else if ( (this.innerHTML == "SKIP") || (this.innerHTML == "WARN") ) {
this.style.backgroundColor = "yellow";
this.style.fontWeight = "bold";
}
} );
}
网格数据样式计时器处理程序:
//Every 2.5 minutes this function will reload all of the grid data.
//It also triggers the boxMethod function.
function timerMethod() {
$("#UpdateStatus_Div").fadeIn(300).delay(1000).fadeOut(300);
RunStatusGrid.clearAndLoad("data/RunStatus.php");
CmdlineGrid.clearAndLoad ("data/CmdlineDataGet.php");
Results0Grid.clearAndLoad ("data/Results0Data.php");
Results1Grid.clearAndLoad ("data/Results1Data.php");
Results2Grid.clearAndLoad ("data/Results2Data.php");
Results3Grid.clearAndLoad ("data/Results3Data.php");
RefreshBoxTimer = setInterval(boxMethod, 2500);
}
//This function searches all of the grids for ERROR/FAIL/SKIP/WARN and changes
//the cell background colors to clearly show where there are issues.
//It will only run 3 times because it is CPU intensive. The 2.5 second delay is because
//the clearAndLoad functions are run as background tasks and don't complete immediately.
var BoxCounter = 0;
function boxMethod() {
BoxCounter++;
if(BoxCounter >= 4) {
BoxCounter = 0;
clearInterval(RefreshBoxTimer);
}
$("td").each(function(index) {
if( (this.innerHTML == "ERROR") || (this.innerHTML == "FAIL")) {
this.style.backgroundColor = "red";
this.style.fontWeight = "bold";
} else if ( (this.innerHTML == "SKIP") || (this.innerHTML == "WARN") ) {
this.style.backgroundColor = "yellow";
this.style.fontWeight = "bold";
}
} );
}
非常感谢您的帮助。使用
setTimeout
而不是setInterval
,并在每次调用回调时重置计时器
setInterval
的问题是,如果计算机忙于做其他事情,那么回调可能会“堆积”,因此它们会立即触发。谢谢。那是个好主意。不过,这仍然会导致从睡眠恢复时出现警报,对吗?当然应该减少警报,但我想知道是否有办法完全停止它。@Anthony记录创建超时的时间,然后检查触发超时的时间。如果差异远远超过了所需的时间,请忽略它。我实施了您的解决方案,它似乎正在工作。非常感谢你的帮助。