Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery.Callbacks() method

jQuery.Callbacks() method

jQuery Miscellaneous MethodsjQuery Miscellaneous Methods

Example

Add callback functions 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 »


Definition and Usage

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

Tip: $.Callbacks is used internally in jQuery, providing foundational functionality for components such as .ajax and $.Deferred. It can also be used in components with similar needs, such as plugins you develop yourself.


Syntax

$

.Callbacks( flags )

Parameters Description
flags Optional. String type: An optional list separated by whitespace, used to change the behavior of the callback list

More examples

The following 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 »


Supported Flags parameters

The flags parameter is an optional argument of $.Callbacks(), structured as an optional list of flags separated by whitespace, used to change the behavior of the callback list (for example, $.Callbacks( 'unique stopOnFalse' )).
The following flags are available:

Parameters Description
once Ensure that this callback list executes only once
memory Cache the argument values from the last fire; when add() adds a callback function, immediately invoke the newly added callback with the previous argument values
unique A callback is added only once and is not added repeatedly
stopOnFalse Interrupt subsequent callbacks after a callback function returns false

The following 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 output: foo

*/

}

)

Try it »

The following 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 »

The following 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 »

The following 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 »

$.Callbacks() supports setting multiple flags in a list rather than just one, with a cumulative effect similar to "&&".
The following 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 »

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 »


$.Callbacks, $.Deferred, and Pub/Sub

The general idea behind pub/sub (the observer pattern) is to promote loose coupling and efficient communication in applications. An observer is also called a subscriber, which refers to the observed object. The publisher notifies users when an event occurs.

As a demonstration of the component-creating capabilities of $.Callbacks(), a Pub/Sub system can be implemented using only a callback function list. Using $.Callbacks as an article queue, you can implement article publishing and subscribing like this:

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 »

A further improvement using $.Deferreds can ensure that a publisher only publishes notifications to subscribers when a specific task has been 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 the mailArrived notification

$.

Topic

(

"

mailArrived

"

)

.

subscribe

(

fn1

)

;

//

Create a new object to replace the deferred $.Deferreds

var

dfd

= $.

Deferred

(

)

;

//

Define a new article (without publishing 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 subscribers is deferred until resolved; it integrates complex procedures as much as possible (such as waiting for an Ajax call to complete), so the message is actually published only once.

*/

//

to complete.

dfd

.

resolve

(

"

Already published!

"

)

;

}

)

Try it »


jQuery Miscellaneous MethodsjQuery Miscellaneous Methods