Javascript函数在等待ajax响应时循环调用

javascript function called on loop while waiting for ajax response

本文关键字:循环 调用 响应 ajax 函数 在等待 Javascript      更新时间:2023-09-26

我有两个.asp脚本-第一个复制sys的内容。表和系统。视图到SQL中的另一个表。第二个脚本计算完成的百分比

(count() from newtable/(count() from sys。Tables + count(*) from sys.views)) * 100

我有一个函数来调用第一个脚本。

function importTablesAndViews()
{
    xmlhttpImportTablesAndViews=GetXmlHttpObject();
    if (xmlhttpImportTablesAndViews==null)
    {
      alert ("Your browser does not support XMLHTTP!");
      return;
    }
    url="importtablesandviews.asp";
    url=url+"?sid="+Math.random();
    xmlhttpImportTablesAndViews.onreadystatechange=function(){if (xmlhttpImportTablesAndViews.readyState==4){
        var aspResponse = xmlhttpImportTablesAndViews.responseText; 
    alert(aspResponse); }}
    xmlhttpImportTablesAndViews.open("GET",url,true);
    xmlhttpImportTablesAndViews.send(null);
}

我有另一个函数来调用进度脚本

function refreshImportProgress()
{
    //alert('Refresh...');
    xmlhttpRefreshImportProgress=GetXmlHttpObject();
    if (xmlhttpRefreshImportProgress==null)
    {
      alert ("Your browser does not support XMLHTTP!");
      return;
    }
    url="importtablesandviewsprogress.asp";
    url=url+"?sid="+Math.random();
    xmlhttpRefreshImportProgress.onreadystatechange=function(){if (xmlhttpRefreshImportProgress.readyState==4){
        var aspResponse = xmlhttpRefreshImportProgress.responseText; document.getElementById('progressDiv').innerHTML = aspResponse; }}
    xmlhttpRefreshImportProgress.open("GET",url,true);
    xmlhttpRefreshImportProgress.send(null);
}

我想要能够做的是调用refreshImportProgress()函数在3秒的间隔从importTablesAndViews()函数。

这可能吗?我试过谷歌同步ajax请求,但到目前为止还没有太多的运气。

xmlhttpRefreshImportProgressonreadystatechange处理器中的refreshImportProgress启动定时器(setTimeout)(只要进度低于100%):

function refreshImportProgress() {
    // ...
    xmlhttpRefreshImportProgress.onreadystatechange = function () {
        if (xmlhttpRefreshImportProgress.readyState == 4) {
            document.getElementById('progressDiv').innerHTML = xmlhttpRefreshImportProgress.responseText;
            var progress = parseInt(xmlhttpRefreshImportProgress.responseText, 10);
                        // ↑ this assumes the value of responseText is a integer only or a integer with a percentage symbol (44%)
            if (progress < 100) {
                setTimeout(refreshImportProgress, 3000);
            }
        }
    }
    // ...
}

importTablesAndViews()中,发送请求后调用refreshImportProgress:

function importTablesAndViews() {
    // ...
    xmlhttpImportTablesAndViews.send(null);
    refreshImportProgress();
}

如果我误解了这个问题,我道歉。但我不确定是否有任何与Ajax有关的3秒计时器是最好的方式。例如,如果你的浏览器非常慢,你就不能保证第一个方法的更改及时完成。另外,对于快速浏览器来说,3秒的等待可能是不必要的。

如果你必须这样做,只需调用setTimeout()http://www.w3schools.com/jsref/met_win_settimeout.asp

似乎更有可能你应该实现一个承诺,当你的问题需要时间解决时调用。https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

甚至是事件侦听器。这是一个清单。http://www.w3schools.com/jsref/dom_obj_event.asp

不应该真的需要使用timeout,除非你只是想延迟异步的事情直到下一个滴答。