这个 javascript setTimeout 以一种非常奇怪的方式与 ajax 请求交互
我正在编写此脚本,以便它显示导入脚本的状态。它应该调用一个函数,每 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
xmlhttp_import_progres.readyState == 4)
仅在请求结束时为true
。因此,完成请求后会弹出警报对话框。此外,您不能期望您的函数在 2 秒间隔后显示警报,因为服务器可能会也可能不会响应得那么快。
最后一点:如果您想要定期更新功能,请使用 setInterval(function(){...}, 2000)。
编辑
另外,以这种方式添加
var
:var xmlhttp_import_progres = GetXMLHttpObject();
。目前,您正在全局定义 HTTP 对象,导致只能访问该 HTTP 对象的一个实例。xmlhttp_import_progres.readyState == 4)
is onlytrue
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.在这里,您可以尝试稍微编辑一下吗:
请考虑上面的答案,但这段代码会让您清楚:
并且
Here, can you try to edit just a little:
Please consider the above answer, but this code will make clear for you:
AND