这个 javascript setTimeout 以一种非常奇怪的方式与 ajax 请求交互

发布于 2024-12-09 03:52:25 字数 1820 浏览 0 评论 0原文

我正在编写此脚本,以便它显示导入脚本的状态。它应该调用一个函数,每 X 秒运行一次 http 请求。

function progres_import() {
//if(import_status != 'finalizat') {
    alert("progres_import");
    setTimeout(function() { return update_progres_import(); }, 2000);
    setTimeout(function() { return update_progres_import(); }, 4000);
    setTimeout(function() { return update_progres_import(); }, 6000);
    setTimeout(function() { return update_progres_import(); }, 8000);

    //setTimeout(function() { progres_import(); }, 400);
//}
//else {

//}
}

这是我用来测试功能的。我添加这些评论也只是为了表明我最终打算用它做什么。我尝试了所有可能的 setTimeout 调用,带引号、不带引号、带或不带匿名函数。

var xmlhttp_import_progres;
function update_progres_import() {
xmlhttp_import_progres=GetXMLHttpObject();
if (xmlhttp_import_progres==null) {
    alert ("Browser does not support HTTP Request (xmlhttp_import_progres)");
    return;
}

var url="crm/ferestre/import_progres.php";  
url=url+"?sid="+Math.random();

xmlhttp_import_progres.onreadystatechange=function() {
    if (xmlhttp_import_progres.readyState == 4) {
        progres_resp = xmlhttp_import_progres.responseText;
        progres = progres_resp.split('_');
        import_nrc = progres[0];
        import_nrt = progres[1];
        import_status = progres[2];
        mesaj = 'Progres import: ' + import_nrc + ' / ' + import_nrt;
        //document.getElementById("corp_import_mesaj").innerHTML = mesaj;
        alert(progres_resp);        
    }
};
xmlhttp_import_progres.open("POST",url,true);
xmlhttp_import_progres.send(null);
}

这是 progres_import 函数的业务端。

发生的情况是,当导入过程开始时,我在第一个函数中收到警报(“progress_import”),但第二个函数中的警报(progres_resp)仅在导入过程结束后才开始弹出(它仍然保持 2 秒)间隔,因此从这个意义上说 setTimeouts 有效)。

ajax 请求中的 php 脚本只接受导入脚本设置的一些会话变量,并打印它们以供 javascript 使用(x 导入 y 总计,z 失败,类似这样的东西)

知道为什么它会这样吗?

I'm writing this script so that it displays the status of an import script. It's supposed to call a function, that runs a http request, every X seconds.

function progres_import() {
//if(import_status != 'finalizat') {
    alert("progres_import");
    setTimeout(function() { return update_progres_import(); }, 2000);
    setTimeout(function() { return update_progres_import(); }, 4000);
    setTimeout(function() { return update_progres_import(); }, 6000);
    setTimeout(function() { return update_progres_import(); }, 8000);

    //setTimeout(function() { progres_import(); }, 400);
//}
//else {

//}
}

this is what i used to test the functionality. I put the comments in too just to show what I intend to ultimately do with it. I tried all the possible setTimeout calls, with quotes, without quotes, with and without the anonymous function.

var xmlhttp_import_progres;
function update_progres_import() {
xmlhttp_import_progres=GetXMLHttpObject();
if (xmlhttp_import_progres==null) {
    alert ("Browser does not support HTTP Request (xmlhttp_import_progres)");
    return;
}

var url="crm/ferestre/import_progres.php";  
url=url+"?sid="+Math.random();

xmlhttp_import_progres.onreadystatechange=function() {
    if (xmlhttp_import_progres.readyState == 4) {
        progres_resp = xmlhttp_import_progres.responseText;
        progres = progres_resp.split('_');
        import_nrc = progres[0];
        import_nrt = progres[1];
        import_status = progres[2];
        mesaj = 'Progres import: ' + import_nrc + ' / ' + import_nrt;
        //document.getElementById("corp_import_mesaj").innerHTML = mesaj;
        alert(progres_resp);        
    }
};
xmlhttp_import_progres.open("POST",url,true);
xmlhttp_import_progres.send(null);
}

this is the business end of the progres_import function.

what happens is i get the alert("progress_import") in the first function right as the import process starts, but the alert(progres_resp) in the second one starts popping up only after the import process is over (it still maintains the 2 second interval so in that sense the setTimeouts worked).

the php script in the ajax request just takes some session variables that the import script sets and prints them for the javascript to use (x imports of y total, z failed, stuff like this)

Any idea why it behaves like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亢潮 2024-12-16 03:52:25

xmlhttp_import_progres.readyState == 4) 仅在请求结束时为 true。因此,完成请求后会弹出警报对话框。

此外,您不能期望您的函数在 2 秒间隔后显示警报,因为服务器可能会也可能不会响应得那么快。

最后一点:如果您想要定期更新功能,请使用 setInterval(function(){...}, 2000)。

编辑

另外,以这种方式添加 varvar xmlhttp_import_progres = GetXMLHttpObject();。目前,您正在全局定义 HTTP 对象,导致只能访问该 HTTP 对象的一个​​实例。

xmlhttp_import_progres.readyState == 4) is only true at the end of the request. Hence, your alert dialogs pop up after finishing the request.

Furthermore, you can't expect your function to show alerts after a 2 second interval, because the server may or may not respond as fast.

A final note: If you want to have a periodical update function, use setInterval(function(){...}, 2000).

EDIT

Also, add var in this way: var xmlhttp_import_progres = GetXMLHttpObject();. Currently, you're globally defining the HTTP object, causing only one instance of the HTTP object to be accessible.

紅太極 2024-12-16 03:52:25

在这里,您可以尝试稍微编辑一下吗:
请考虑上面的答案,但这段代码会让您清楚:


function progres_import() {
//if(import_status != 'finalizat') {
    alert("progres_import");
    setTimeout(function() { return update_progres_import(0); }, 2000);
    setTimeout(function() { return update_progres_import(1); }, 4000);
    setTimeout(function() { return update_progres_import(2); }, 6000);
    setTimeout(function() { return update_progres_import(3); }, 8000);

    //setTimeout(function() { progres_import(); }, 400);
//}
//else {

//}
}

并且

var xmlhttp_import_progres = [];
function update_progres_import(i) {
    xmlhttp_import_progres[i]= GetXMLHttpObject();
    if (xmlhttp_import_progres[i]==null) {
        alert ("Browser does not support HTTP Request (xmlhttp_import_progres)");
        return;
    }

    var url="crm/ferestre/import_progres.php";  
    url=url+"?sid="+Math.random();

    xmlhttp_import_progres[i].onreadystatechange=function() {
        if (xmlhttp_import_progres[i].readyState == 4) {
            progres_resp = xmlhttp_import_progres[i].responseText;
            progres = progres_resp.split('_');
            import_nrc = progres[0];
            import_nrt = progres[1];
            import_status = progres[2];
            mesaj = 'Progres import: ' + import_nrc + ' / ' + import_nrt;
            //document.getElementById("corp_import_mesaj").innerHTML = mesaj;
            alert(progres_resp);        
        }
    };
    xmlhttp_import_progres[i].open("POST",url,true);
    xmlhttp_import_progres[i].send(null);
}

Here, can you try to edit just a little:
Please consider the above answer, but this code will make clear for you:


function progres_import() {
//if(import_status != 'finalizat') {
    alert("progres_import");
    setTimeout(function() { return update_progres_import(0); }, 2000);
    setTimeout(function() { return update_progres_import(1); }, 4000);
    setTimeout(function() { return update_progres_import(2); }, 6000);
    setTimeout(function() { return update_progres_import(3); }, 8000);

    //setTimeout(function() { progres_import(); }, 400);
//}
//else {

//}
}

AND

var xmlhttp_import_progres = [];
function update_progres_import(i) {
    xmlhttp_import_progres[i]= GetXMLHttpObject();
    if (xmlhttp_import_progres[i]==null) {
        alert ("Browser does not support HTTP Request (xmlhttp_import_progres)");
        return;
    }

    var url="crm/ferestre/import_progres.php";  
    url=url+"?sid="+Math.random();

    xmlhttp_import_progres[i].onreadystatechange=function() {
        if (xmlhttp_import_progres[i].readyState == 4) {
            progres_resp = xmlhttp_import_progres[i].responseText;
            progres = progres_resp.split('_');
            import_nrc = progres[0];
            import_nrt = progres[1];
            import_status = progres[2];
            mesaj = 'Progres import: ' + import_nrc + ' / ' + import_nrt;
            //document.getElementById("corp_import_mesaj").innerHTML = mesaj;
            alert(progres_resp);        
        }
    };
    xmlhttp_import_progres[i].open("POST",url,true);
    xmlhttp_import_progres[i].send(null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文