![](/img/trans.png)
[英]how to prevent to send ajax request while another ajax request is processing?
[英]Does Ajax block the window while processing the request?
我正在发出ajax请求,可能需要很多时间来处理服务器端。所以我想在请求过程中显示一个加载图像。但是在ajax请求期间没有显示加载图像。
var ref = createAjaxRequest();//Ajax req is created here...
if(ref){
showLoadingImg();
ref.open('POST','x.jsp',false);
ref.onreadystatechange = eventStateChange;
ref.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ref.setRequestHeader("Connection", "keep-alive");
ref.send();
}
else{ alert("Your browser does not support this feature");
}
function eventStateChange(){
if(ref.readyState==4 ){
//processing response here......
hideLoadingImg();
}
}
function showLoadingImg();{
/* <div id="ajaxLoading">(with background image is in page)
It is displayed properly when I set display:inline
manually through developer tools of a browser.</div>
*/
document.getElementById('ajaxLoading').style.display='inline';
}
function hideLoadingImg();{
document.getElementById('ajaxLoading').style.display='none';
}
有什么问题吗?
我尝试调试,发现:
尽管showLoadingImg()
在open()
方法之前被调用,但是加载图像仅在ref.readyState==2
之后显示在浏览器上。
但是不幸的是, readyState==2
和readyState==4
之间的时间间隔很小,正在加载的图像立即被隐藏。
因此用户看不到加载图像...
因此,我怀疑的是,除非ajax运行到readyState==2
否则它不会运行脚本。
如果您像在此处使用第三个参数那样将async
设置为false
,则XMLHttpRequest会阻塞: ref.open('POST','x.jsp',false);
。
我认为您open
电话是错误的。 第三个参数(布尔值)指示请求是否异步。
在这里考虑完整的文档: http : //www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
ref.open('POST','x.jsp',true);
应该解决你的问题。
问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.