jQuery Message Queuing: Get all your JavaScript ducks in a row

Version: 1.0, Last updated: 1/5/2010

Project Homehttp://benalman.com/projects/jquery-message-queuing-plugin/
GitHubhttp://github.com/cowboy/jquery-message-queuing/
Sourcehttp://github.com/cowboy/jquery-message-queuing/raw/master/jquery.ba-jqmq.js
(Minified)http://github.com/cowboy/jquery-message-queuing/raw/master/jquery.ba-jqmq.min.js (1.2kb)
Summary
jQuery Message Queuing: Get all your JavaScript ducks in a rowVersion: 1.0, Last updated: 1/5/2010
LicenseCopyright © 2010 “Cowboy” Ben Alman, Dual licensed under the MIT and GPL licenses.
ExamplesThese working examples, complete with fully commented code, illustrate a few ways in which this plugin can be used.
Support and TestingInformation about what version or versions of jQuery this plugin has been tested with, what browsers it has been tested in, and where the unit tests reside (so you can test it yourself).
Release History
Functions
jQuery.jqmqCreate a new queue.
queueObj.addAdd a single item onto the queue.
queueObj.addEachAdd multiple items onto the queue, individually.
queueObj.startStart a currently paused queue.
queueObj.nextIntended to be called from within the jQuery.jqmq callback, this method will continue a queue with a delay of -1.
queueObj.clearClear a queue completely.
queueObj.pausePause a currently running queue.
queueObj.updateUpdate an existing queue’s options.
queueObj.sizeGet the current queue length.
queueObj.indexOfGet the current index in the queue of the passed item.
jQuery.fn.jqmqAddAdd a jQuery collection of elements onto the queue.
jQuery.fn.jqmqAddEachAdd each selected element from a jQuery collection onto the queue, individually.

License

Copyright © 2010 “Cowboy” Ben Alman, Dual licensed under the MIT and GPL licenses.  http://benalman.com/about/license/

Examples

These working examples, complete with fully commented code, illustrate a few ways in which this plugin can be used.

Serial AJAXhttp://benalman.com/code/projects/jquery-message-queuing/examples/ajax/
Throttlinghttp://benalman.com/code/projects/jquery-message-queuing/examples/throttling/

Support and Testing

Information about what version or versions of jQuery this plugin has been tested with, what browsers it has been tested in, and where the unit tests reside (so you can test it yourself).

jQuery Versions1.3.2, 1.4a2
Browsers TestedInternet Explorer 6-8, Firefox 2-3.7, Safari 3-4, Chrome, Opera 9.6-10.
Unit Testshttp://benalman.com/code/projects/jquery-message-queuing/unit/

Release History

1.0(1/5/2010) Initial release

Functions

jQuery.jqmq

Create a new queue.

Usage

var queueObj = jQuery.jqmq( options );

Arguments

options(Object) An object containing options specific to this queue.

Options

delay(Number) Time in milliseconds between each callback execution.  If delay is -1, queue will wait for a queueObj.next call instead of auto-executing.  Defaults to 100.
batch(Number) Number of queue items to process at a time.  If less than this number of items remain in the queue, the remainder will be processed.  Defaults to 1.
queue(Array) Populate the queue initially with these items.  Defaults to an empty initial queue.
callback(Function) Called for each queue item or batch of items, every delay milliseconds.  This function is passed a single argument, which is the single queue item if batch is 1, or an array of queue items if batch is > 1.  If callback returns true, the queue item(s) will be re- added back onto the front of the queue for the next callback execution to retry.  Inside this function, `this` refers to the queueObj object.
complete(Function) Called whenever there are no longer any queue items to process.  After completion, if more queue items are added and the queue completes again, this function will be called again.  Inside this function, `this` refers to the queueObj object.
paused(Boolean) If true, initialize this queue in a paused state.  Defaults to false.

Returns

(Object) a reference to the jqmq queue object.

queueObj.add

Add a single item onto the queue.  If you want to add multiple items onto the queue individually, use queueObj.addEach.  If the queue was empty and not paused, processing will resume immediately.

Usage

queueObj.add( item [, priority ] );

Arguments

item(Anything) A single item to add to the queue.
priority(Boolean) If true, the item is added to the front of the queue, otherwise the item is added to the end of the queue.  Defaults to false.

Returns

(Number) The length of the queue, after the item has been added.

queueObj.addEach

Add multiple items onto the queue, individually.  If you want to add a single item onto the queue, use queueObj.add.  If the queue was empty and not paused, processing will resume immediately.

Usage

queueObj.addEach( items [, priority ] );

Arguments

items(Array) An array of items to add to the queue.
priority(Boolean) If true, the items are added to the front of the queue, otherwise the items are added to the end of the queue.  Defaults to false.

Returns

(Number) The length of the queue, after the items have been added.

queueObj.start

Start a currently paused queue.  If an empty queue is started, it will automatically start processing items as soon as they are added.

Usage

queueObj.start();

Returns

Nothing.

queueObj.next

Intended to be called from within the jQuery.jqmq callback, this method will continue a queue with a delay of -1.  This is most useful for queues of asynchronous-but-serial actions, like AJAX requests that must execute in order, but not overlap.

Usage

queueObj.next( [ retry ] );

Arguments

retry(Boolean) If true, the queue item(s) will be re-added back to the front of the queue to be retried on the next queue execution.

Returns

Nothing.

queueObj.clear

Clear a queue completely.  The paused/started status of the queue is unchanged.

Usage

queueObj.clear();

Returns

(Array) The previous queue contents.

queueObj.pause

Pause a currently running queue.  A paused but empty queue will need to be manually restarted with queueObj.start even after new items are added.

Usage

queueObj.pause();

Returns

Nothing.

queueObj.update

Update an existing queue’s options.

Usage

queueObj.update( options );

Arguments

options(Object) An object containing options specific to this queue.

Options

The delay, batch, callback and complete options from jQuery.jqmq can be updated.  The queue and paused state can be changed using the other queueObj methods.

Returns

Nothing.

queueObj.size

Get the current queue length.

Usage

queueObj.size();

Returns

(Number) The length of the queue.

queueObj.indexOf

Get the current index in the queue of the passed item.

Usage

queueObj.indexOf( item );

Arguments

item(Anything) An item to test the index of.

Returns

(Number) The index of the passed item in the queue.  Returns -1 if not found.

jQuery.fn.jqmqAdd

Add a jQuery collection of elements onto the queue.  If you want to add each selected element onto the queue individually, use jQuery.fn.jqmqAddEach.  If the queue was empty and not paused, processing will resume immediately.

Usage

jQuery('selector').jqmqAdd( queueObj [, priority ] );

Arguments

queueObj(Object) A valid jqmq object, returned from jQuery.jqmq.
priority(Boolean) If true, the item is added to the front of the queue, otherwise the item is added to the end of the queue.  Defaults to false.

Returns

(jQuery) The initial jQuery collection of elements.

jQuery.fn.jqmqAddEach

Add each selected element from a jQuery collection onto the queue, individually.  If you want to add the entire jQuery collection of elements onto the queue as a single item, use jQuery.fn.jqmqAdd.  If the queue was empty and not paused, processing will resume immediately.

Usage

jQuery('selector').jqmqAddEach( queueObj [, priority ] );

Arguments

queueObj(Object) A valid jqmq object, returned from jQuery.jqmq.
priority(Boolean) If true, the items are added to the front of the queue, otherwise the items are added to the end of the queue.  Defaults to false.

Returns

(jQuery) The initial jQuery collection of elements.

Create a new queue.
Intended to be called from within the jQuery.jqmq callback, this method will continue a queue with a delay of -1.
Add multiple items onto the queue, individually.
Add a single item onto the queue.
Start a currently paused queue.
Add each selected element from a jQuery collection onto the queue, individually.
Add a jQuery collection of elements onto the queue.
Close