如何检查以前的通话中ajax功能是否忙碌?如果ajax函数的呼叫是readyState != 4
,那么我如何阻止调用ajax函数呢?
答案 0 :(得分:5)
您可以使用布尔值和适当的onreadystatechange函数;
var inprogress = true;
ajaxRequest = ...
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
inprogress = false;
}
}
答案 1 :(得分:0)
问题是,如果您之前的呼叫仍然忙碌,是否要跳过Ajax呼叫?如果您想跳过它,解决方案很简单,Matthew的一个非常适合您的需求。但是,如果您想在上一个完成后开始执行调用,那就完全不同了。如果需要,我可以为您提供一些代码。
一直致力于解决方案:
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>
<script language="javascript" type="text/javascript">
var requestInProcess = false;
var timer = null;
function requestSomeStuffA() {
var responseHolder = $("responseHolder");
new Ajax.Request(
"http://webservicelocation/",
{
method: 'GET',
contentType: 'application/json; charset=utf-8',
onCreate: function() {
requestInProcess = true;
},
onSuccess: function(transport) {
//do something with the transport
},
onFailure: function(error) {
//throw a nice exception
},
onComplete: function() {
requestInProcess = false;
}
}
);
}
function requestSomeStuffB() {
clearTimeout(timer);
if(requestInProcess) {
//wait again for a while
timer = window.setTimeout("requestSomeStuffB()", 10); //timeout in miliseconds
}
else{
//execute the next Ajax request
}
}
</script>
当您首先调用requestSomeStuffA然后再调用requestSomeStuffB时,requestSomeStuffB将等待requestSomeStuffA完成。希望这对你有用。