Javascript: xmlhttpRequest Object How to handle one asynchronous request at a time
I have a question on the xmlhttprequest object. Let's say I create a xmlhttprequest object X to handle a user's request(asynchronously) to the server. For some reason, the server take 15 seconds to response. During those 15 seconds, if the user click on another button, how am I going about to determi开发者_运维知识库ne the existence of the the first xmlhttprequest object I created in order to act accordingly (either telling the user that he/she needs to wait for the first request to finish or cancel it and continue with the second one.)
In short term, I need to know whether there is any request is being processed in order to handle another request.
Thank you in advance.
You can set a flag in the initial call and then clear it in the response. Be sure to also clear it in the error callback if applicable to the framework you're using.
var callInProgress = false;
...
function callSever() {
callInProgress = true;
...
}
function responseHandler() {
...
callInProgress = false;
}
function errorHandler() {
...
callInProgress = false;
}
精彩评论