Advertisement
rplantiko

Queued Ajax Requests in jQuery

Jul 3rd, 2013
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Request Queueing in jQuery
  2. // Ensure on client side that one request is processed after the former has been completed
  3. // Any request should be automatically queued, with no changes in the $.post( ) and $.ajax( ) calls
  4.  
  5. /* For this, register special queue functions in jQuery.ajaxSetup() :
  6.  
  7.  $.ajaxSetup({
  8.    "beforeSend":ajaxQueue.beforeSend,
  9.    "complete":ajaxQueue.complete    
  10.    })
  11.  
  12. */
  13.  
  14. var ajaxQueue =  (function() {
  15.   var q = [];
  16.   var executingNext = false;
  17.   return {
  18.     beforeSend:function(jqXHR,opt) {
  19.       if ($.active > 1 && ! executingNext ) {
  20.         q.push(opt);
  21.         executingNext = false;
  22.         return false;
  23.         }
  24.       },
  25.     complete:function() {
  26.       if (q.length > 0) {
  27.         var $next = q.shift( );
  28.         executingNext = true;
  29.         $.ajax( $next );
  30.         }
  31.       },
  32.     get:function() {  // --> for debug purposes
  33.       return q;
  34.       }
  35.     };
  36.  })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement