AjaxQueue loading icon
I am using the following code to queue ajax requests, not block actions on the page. The problem is that I need to show a loading icon to indicate that the queue has not finished executing. I can't figure out how to do this and everything I've tried just closes immediately.
// jQuery on an empty object, we are going to use this as our Queue
var ajaxQueue = $jq({});
$jq.ajaxQueue = function( ajaxOpts ) {
var jqXHR,
dfd = $jq.Deferred(),
promise = dfd.promise();
// queue our ajax request
ajaxQueue.queue( doRequest );
// add the abort method
promise.abort = function( statusText ) {
// proxy abort to the jqXHR if it is active
if ( jqXHR ) {
return jqXHR.abort( statusText );
}
// if there wasn't already a jqXHR we need to remove from queue
var queue = ajaxQueue.queue(),
index = $jq.inArray( doRequest, queue );
if ( index > -1 ) {
queue.splice( index, 1 );
}
// and then reject the deferred
dfd.rejectWith( ajaxOpts.context || ajaxOpts,
[ promise, statusText, "" ] );
return promise;
};
// run the actual query
function doRequest( next ) {
jqXHR = $jq.ajax( ajaxOpts )
.done( dfd.resolve )
.fail( dfd.reject )
.then( next, next );
}
return promise;
};
+3
source to share
1 answer
You can create a div on the page to hold the icon. In this case, I would use the gif loading icon. When you first run ajax on the page use .show () to display it then inside your .done use .hide ()
Similar to this post How to display the loading gif until the whole html page is loaded
0
source to share