同时发两个Ajax异步请求,返回的结果混乱了。
时间:2011-09-21
来源:互联网
var is_make_completed = false;
var make_cont_count = 0;
这是一个Ajax请求,循环发送,一秒钟一次,称为A请求
function loadSccContent()
{
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("scc_content:"+xmlhttp.responseText);
if (document.getElementById("compiler_output").innerHTML != xmlhttp.responseText) {
document.getElementById("compiler_output").innerHTML = xmlhttp.responseText;
make_cont_count = 0;
} else {
make_cont_count += 1;
}
return ;
//document.getElementById("compiler_output").value=xmlhttp.responseText;
}
}
branch_id = document.getElementById("compiler_compilerBranch").value
xmlhttp.open("GET","/ajax/scccontent/{{user.name}}/"+branch_id+"?t=" + Math.random(),true);
xmlhttp.setRequestHeader("If-Modified-Since","0");
xmlhttp.send();
}
function timeoutMake()
{
//alert(make_cont_count);
loadSccContent();
if ((is_make_completed == false)&&(make_cont_count < 100)) {
setTimeout("timeoutMake()",1000);
}
}
下面是第二个请求,只发送一次,返回结果本来应该是几分钟以后,称为B请求。
function makeBranchAjax()
{
is_make_completed = false;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//alert(xmlhttp.responseText);
alert("make was completetd !");
is_make_completed = true;
return ;
}
}
branch_id = document.getElementById("compiler_compilerBranch").value
xmlhttp.open("GET","/ajax/make/{{user.name}}/"+branch_id+"?t=" + Math.random(),true);
xmlhttp.setRequestHeader("If-Modified-Since","0");
xmlhttp.send();
}
同时调用了两个函数,两个都是异步,A请求能够正常运行并收到结果,但是B请求等待一段时间后收到的结果却是A请求返回的结果,并且一连收到了3遍。
function updateAndMakeAjax()
{
makeBranchAjax();
timeoutMake();
}
如果只运行B请求,也能收到正确结果。或者B请求同步,也可以收到正确结果,但两个同时请求结果就混乱了。
var make_cont_count = 0;
这是一个Ajax请求,循环发送,一秒钟一次,称为A请求
function loadSccContent()
{
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("scc_content:"+xmlhttp.responseText);
if (document.getElementById("compiler_output").innerHTML != xmlhttp.responseText) {
document.getElementById("compiler_output").innerHTML = xmlhttp.responseText;
make_cont_count = 0;
} else {
make_cont_count += 1;
}
return ;
//document.getElementById("compiler_output").value=xmlhttp.responseText;
}
}
branch_id = document.getElementById("compiler_compilerBranch").value
xmlhttp.open("GET","/ajax/scccontent/{{user.name}}/"+branch_id+"?t=" + Math.random(),true);
xmlhttp.setRequestHeader("If-Modified-Since","0");
xmlhttp.send();
}
function timeoutMake()
{
//alert(make_cont_count);
loadSccContent();
if ((is_make_completed == false)&&(make_cont_count < 100)) {
setTimeout("timeoutMake()",1000);
}
}
下面是第二个请求,只发送一次,返回结果本来应该是几分钟以后,称为B请求。
function makeBranchAjax()
{
is_make_completed = false;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//alert(xmlhttp.responseText);
alert("make was completetd !");
is_make_completed = true;
return ;
}
}
branch_id = document.getElementById("compiler_compilerBranch").value
xmlhttp.open("GET","/ajax/make/{{user.name}}/"+branch_id+"?t=" + Math.random(),true);
xmlhttp.setRequestHeader("If-Modified-Since","0");
xmlhttp.send();
}
同时调用了两个函数,两个都是异步,A请求能够正常运行并收到结果,但是B请求等待一段时间后收到的结果却是A请求返回的结果,并且一连收到了3遍。
function updateAndMakeAjax()
{
makeBranchAjax();
timeoutMake();
}
如果只运行B请求,也能收到正确结果。或者B请求同步,也可以收到正确结果,但两个同时请求结果就混乱了。
作者: qianbbo 发布时间: 2011-09-21
试试:
xmlhttpA = new XMLHttpRequest();
...
xmlhttpB = new XMLHttpRequest();
xmlhttpA = new XMLHttpRequest();
...
xmlhttpB = new XMLHttpRequest();
作者: slowhand 发布时间: 2011-09-21
继续路过~
作者: trista_career 发布时间: 2011-09-21
引用 1 楼 slowhand 的回复:
试试:
xmlhttpA = new XMLHttpRequest();
...
xmlhttpB = new XMLHttpRequest();
试试:
xmlhttpA = new XMLHttpRequest();
...
xmlhttpB = new XMLHttpRequest();
这样竟然真可以。但是明明是局部变量,为什么会互相影响,公用?
作者: qianbbo 发布时间: 2011-09-21
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28