开发者

How to detect running asynchronous AJAX call and put another one in queue in jQuery?

开发者 https://www.devze.com 2023-03-15 06:34 出处:网络
I have PhoneGap app in Android using jQuery. Sometimes I need to upload some larger amount of data using $.post asynchronously, so the data are sent in background. That works fine, but it on slower co

I have PhoneGap app in Android using jQuery. Sometimes I need to upload some larger amount of data using $.post asynchronously, so the data are sent in background. That works fine, but it on slower connection it make take some time. I have found out, that firing another asynchronous AJAX call will cause abort to the first call.

Is there a way h开发者_StackOverflowow to detect if there is some asynchronous AJAX in progress and if YES, than how to put another asynchronous AJAX call into queue and start it when the first one finishes ?


Another option would be to save a reference to the jqXHR object and you can then call the complete method to see if it has completed.

var jqXHR = $.post(url,data,function()
{

}); 

if(jqXHR.complete())
{
    //do second call
}

All of the above is discussed on the man page http://api.jquery.com/jQuery.post/

You could work on this and add a timer. Then if it's not complete just set a timer for a few seconds and check again if it has yet completed.


Regarding detecting an async AJAX request in progress, you could use some variable that is initialized with some value when a call starts and is set to a different value when the call finishes.

For the second part, if that particular variable hasn't been set, add your function call to some queue that you process with a handler for the 'ajaxComplete' event. Alternatively, pass in a callback via .complete() that does something along those lines.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号