jQuery.Callbacks()Method

jQuery 杂项方法jQuery Miscellaneous Methods

Examples

Add a callback function to the $.Callbacks list

$(function () { function fn1( value ) { alert( value ); } function fn2( value ) { fn1("fn2 says: " + value); return false; } var callbacks = $.Callbacks(); callbacks.add( fn1 ); //Output: foo! callbacks.fire( "foo!" ); callbacks.add( fn2 ); //Output: bar!, fn2 says: bar! callbacks.fire( "bar!" ); })

Try it Yourself »

Definition and Usage

$.Callbacks() refers to a multi-purpose callback function list object that provides a powerful way to manage callback function queues.

Tip:$.Callbacks is used internally in jQuery, such as functions that provide basic functionality for components like .ajax, $.Deferred, etc. It can also be used in some components with similar functionality, such as self-developed plugins.


Syntax

$.Callbacks( flags )

Parameters Description
flags Optional. String type. An optional list separated by spaces to change the behavior of the callback list.


实例

More Examples

Below is an example of using .remove() to delete a specific callback from the callback list.

Example

$(function () { function fn1( value ) { alert( value ); } function fn2( value ) { fn1("fn2 says: " + value); return false; } var callbacks = $.Callbacks(); callbacks.add( fn1 ); //Output: foo! callbacks.fire( "foo!" ); callbacks.add( fn2 ); //Output: bar!, fn2 says: bar! callbacks.fire( "bar!" ); callbacks.remove( fn2 ); //Only foobar is output; fn2 has been removed. callbacks.fire( "foobar" ); })

Try it Yourself »


Supported Flags Parameters

This flags parameter is an optional parameter of $.Callbacks(). It is structured as an optional list of flags separated by spaces to change the behavior of the callback list (e.g. $.Callbacks( 'unique stopOnFalse' )).
The following flags are available:

Parameters Description
once Ensure this callback list is executed only once.
memory Cache the parameter values from the last fire. When add() adds a callback function, immediately call the newly added callback function with the last parameter values.
unique A callback can only be added once and will not be added repeatedly. stopOnFalse After a callback function returns false, interrupt the subsequent callback functions.

Below is an example of $.Callbacks( "once" ).

Example

$(function () { function fn1( value ) { alert( value ); } function fn2( value ) { fn1("fn2 says: " + value); return false; } var callbacks = $.Callbacks( "once" ); callbacks.add( fn1 ); callbacks.fire( "foo" ); callbacks.add( fn2 ); callbacks.fire( "bar" ); callbacks.remove( fn2 ); callbacks.fire( "foobar" ); /*Only outputs: foo*/ })

Try it Yourself »

Below is an example of $.Callbacks( "memory" ).

Example

$(function () { function fn1( value ) { alert( value ); } function fn2( value ) { fn1("fn2 says: " + value); return false; } var callbacks = $.Callbacks( "memory" ); callbacks.add( fn1 ); callbacks.fire( "foo" ); callbacks.add( fn2 ); callbacks.fire( "bar" ); callbacks.remove( fn2 ); callbacks.fire( "foobar" ); /*Output: foo fn2 says:foo bar fn2 says:bar foobar*/ })

Try it Yourself »

Below is an example of $.Callbacks( "unique" ).

Example

$(function () { function fn1( value ) { alert( value ); } function fn2( value ) { fn1("fn2 says: " + value); return false; } var callbacks = $.Callbacks( "unique" ); callbacks.add( fn1 ); callbacks.fire( "foo" ); callbacks.add( fn1 ); // repeat addition callbacks.add( fn2 ); callbacks.fire( "bar" ); callbacks.remove( fn2 ); callbacks.fire( "foobar" ); /*Output: foo bar fn2 says:bar foobar*/ })

Try it Yourself »

Below is an example of $.Callbacks( "stopOnFalse" ).

Example

$(function () { function fn1( value ) { alert( value ); return false; } function fn2( value ) { fn1( "fn2 says: " + value ); return false; } var callbacks = $.Callbacks( "stopOnFalse" ); callbacks.add( fn1 ); callbacks.fire( "foo" ); callbacks.add( fn2 ); callbacks.fire( "bar" ); callbacks.remove( fn2 ); callbacks.fire( "foobar" ); /*Output: foo bar foobar*/ })

Try it Yourself »

$.Callbacks() supports setting multiple flags (identifiers) in a list rather than just one, and has a cumulative effect, similar to "&&".
Below is an example of $.Callbacks( 'unique memory' ).

Example

$(function () { function fn1( value ) { alert( value ); return false; } function fn2( value ) { fn1( "fn2 says: " + value ); return false; } var callbacks = $.Callbacks( "unique memory" ); callbacks.add( fn1 ); callbacks.fire( "foo" ); callbacks.add( fn1 ); // repeat addition callbacks.add( fn2 ); callbacks.fire( "bar" ); callbacks.add( fn2 ); callbacks.fire( "baz" ); callbacks.remove( fn2 ); callbacks.fire( "foobar" ); /*Output: foo fn2 says:foo bar fn2 says:bar baz fn2 says:baz foobar*/ })

Try it Yourself »

The $.Callbacks method can also be separated, for example:

Example

$(function () { function fn1( value ) { alert( value ); } var callbacks = $.Callbacks(), add = callbacks.add, remove = callbacks.remove, fire = callbacks.fire; add( fn1 ); fire( "hello world" ); remove( fn1 ); /*Output: hello world*/ })

Try it Yourself »


$.Callbacks, $.Deferred and Pub/Sub

The general idea behind pub/sub (observer pattern) is to promote loose coupling and efficient communication in applications. Observers, also called subscribers, point to the observed subject. The Publisher notifies subscribers when an event occurs.

As a demonstration of the components created by $.Callbacks(), you can implement a Pub/Sub system using only the callback function list. Using $.Callbacks as a topic queue, you can publish and subscribe to topics as follows:

Example

$(function () { function fn1( value ) { alert( value ); return false; } function fn2( value ) { fn1( "fn2 says: " + value ); return false; } var topics = {}; jQuery.Topic = function( id ) { var callbacks, method, topic = id && topics[ id ]; if ( !topic ) { callbacks = jQuery.Callbacks(); topic = { publish: callbacks.fire, subscribe: callbacks.add, unsubscribe: callbacks.remove }; if ( id ) { topics[ id ] = topic; } } return topic; }; //Subscriber $.Topic( "mailArrived" ).subscribe( fn1 ); $.Topic( "mailArrived" ).subscribe( fn2 ); $.Topic( "mailSent" ).subscribe( fn1 ); //Publisher $.Topic( "mailArrived" ).publish( "hello world!" ); $.Topic( "mailSent" ).publish( "woo! mail!" ); /*Output: hello world! fn2 says: hello world! woo! mail!*/ })

Try it Yourself »

A further improvement using $.Deferreds can ensure that the publisher only publishes notifications to subscribers when a specific task is completed (or resolved). See the example code below:

Example

$(function () { function fn1( value ) { alert( value ); return false; } function fn2( value ) { fn1( "fn2 says: " + value ); return false; } var topics = {}; jQuery.Topic = function( id ) { var callbacks, method, topic = id && topics[ id ]; if ( !topic ) { callbacks = jQuery.Callbacks(); topic = { publish: callbacks.fire, subscribe: callbacks.add, unsubscribe: callbacks.remove }; if ( id ) { topics[ id ] = topic; } } return topic; }; //Subscribe to mailArrived notifications $.Topic( "mailArrived" ).subscribe( fn1 ); //Create a new object to replace the deferred $.Deferreds var dfd = $.Deferred(); //Define a new topic (not published directly) var topic = $.Topic( "mailArrived" ); //When the deferred is resolved, publish a notification to subscribers dfd.done( topic.publish ); /*Here, the message to be passed back to the subscriber is deferred until it is resolved. It integrates complex procedures as much as possible (e.g., waiting for an Ajax call to complete), so in fact the message is only published once.*/ //Done. dfd.resolve( "Already published!" ); })

Try it Yourself »


jQuery 杂项方法jQuery Miscellaneous Methods

Other Extensions