-1

我有一个长时间运行的数据库更新,我想在它发生时报告它。我启动间隔时间我已经看到了很多关于 setInterval 问题的例子,它看起来很简单,但我无法用这段代码来触发它。checkSaveStatus 代码拉回一个状态报告,该报告告诉您已保存了多少行。一旦我可以使基本功能正常工作,我将在显示结果后对其进行增强。

我开始怀疑这可能与我处理 Ajax 调用的方式有关。

var checkTimer;
function checkSaveStatus() {
    var saveStatus = ajaxCall("WebServices/FLSAService.asmx/CheckFLSASaveStatus");
    if (saveStatus == null) {
        clearInterval(checkTimer);
        return;
    }
    log('saveStatus: ' + saveStatus.InputCount + '/' + saveStatus.ResultCount + ':' + saveStatus.SaveComplete, 'FLSA');
    if (saveStatus.SaveComplete) {
        reportSaveComplete = true;
        clearInterval(checkTimer);
    }
}

checkTimer = window.setInterval(checkSaveStatus, 1000);
... make an asynchronous Ajax call that takes a long time to complete
... the checkSaveStatus code doesn't fire until the Ajax call completes
4

1 回答 1

1

As far as I know, you cannot return a value from your ajax because it is asynchronous. The way that is handled is through callbacks. So this section of code:

var saveStatus = ajaxCall("WebServices/FLSAService.asmx/CheckFLSASaveStatus");
if (saveStatus == null) {
    clearInterval(checkTimer);
    return;
}

Is more than likely not operating properly. It is hard to say without seeing the ajaxCall function, but you are probably not getting the result you intended to get. That left hand side assignment is not going to reflect anything which would be asynchronous. As a result it will either be what the default value is returned from ajaxCall, or it will be undefined, which when compared with == null will be true, and in turn will clear your interval and return.

于 2013-05-23T22:51:13.140 回答