计算机从睡眠状态恢复后,Javascript计时器会导致过多警报

时间:2012-03-15 14:46:41

标签: javascript sleep alert setinterval

我的网页上有数据网格需要每隔几分钟刷新一次。当Javascript计时器到期时,使用PHP从mysql数据库查询数据。导入数据后,需要对其进行样式设置以使问题容易可见,因此我会触发另一个计时器,该计时器将每2.5秒翻转一次,共计三次,然后关闭。我提出了这个解决方案,因为没有立即填充数据网格,所以在查询后运行它没有延迟导致它错过了大部分数据。

问题: 1)最大的问题是,当计算机进入睡眠状态时,如果网页在浏览器中保持打开状态,则会有几个空警报。鉴于有多少人将在工作中使用此网站,这种行为是完全不可接受的。我没有想法。

2)我希望在导入时有更好的方法来处理数据,所以我不需要$(“td”)。每个()刷新后的整个页面。我正在使用dhtmlx库中的组件,因此有点难以打开函数并在内部处理它。对数据进行后处理是我真正能做到的所有工作,但我对这些想法持开放态度。


变量声明和初始计时器设置:

//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";
        }
    } );
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

使用setTimeout代替setInterval,并在每次调用回调时重置计时器。

setInterval的问题在于,如果计算机太忙于执行其他操作,则回调最终会“堆叠”,因此它们会立即触发。