Queued Ajax for jQuery
by Ruben Holthuijsen
CLICK HERE FOR A LIVE DEMO (mirror)
This jQuery plugin adds a new $.qAjax function. The main difference is that requests made with $.qAjax will be queued: arequest will only be made after the last request is finished. This guarantees that requests will finish in order.
Current version: 21.7.19 (changelog)
Requires jQuery 1.5.0 or above
Include jquery-qajax.js after jQuery, and use $.qAjax just as you would $.ajax. Additionally, you can optionally provide qKey and qTimeout arguments to the Ajax settings.
As $.ajax will not be called immediately, you can't manipulate the returned jqXHR object such as with .done, .fail, and .always. Instead of $.ajax('/some-request', { ... }).done(function (data) { ... }), you can use $.qAjax('/some-request', { success: function (data) { ... }, ... }); similarly with error instead of .fail and complete instead of .always. For advanced cases, you can use beforeSend to manipulate the jqXHR directly. See jQuery's documentation on $.ajax for more options.
In addition to all the settings for jQuery's own $.ajax function, you can use add following extra settings to the Ajax settings object:
(added in 21.7.3)
A key (string) to identify the request: When a keyed request is added to the queue, any previously queued requests with the same key will be skipped. Note that requests that are already running might still need to finish and return their result later.
(added in 21.7.19)
An optional timeout (in milliseconds): When given, this timeout will be applied before the request is added to the queue. This can be used to prevent the request from firing when a newer request has been made with the same qKey.