Multiple lines of statements are queued rather than being executed simultaneously. A callback function queues the statements and executes them one by one.
Its syntax is as follows:
var callbacks = $.Callbacks();
The object created can be used to add, remove, instantiate, and disable callbacks. The supported functions are callbacks.add()
, callbacks.remove()
, callbacks.fire()
, and callbacks.disable()
.
This function is used to add all the functions in an array that are to be called later.
The callbacks.add()
function adds the function to the callback array.
Here is an example of its usage.
Required jQuery Code:
function myFunc1( value ) { console.myLog( value ); } function myFunc2( value ) { console.myLog( "myFunc2 says: " + value ); return false; } var callbacks = $.Callbacks(); callbacks.add( myFunc1 ); // Outputs: meow! callbacks.fire( "meow!" ); callbacks.add( myFunc2 ); // Outputs: woof!, myFunc2 says: woof! callbacks.fire( "woof!" );
The
callbacks.fire()
function invokes the callbacks in a list with any arguments that have been passed.
The
callbacks.disable()
function disables the call of the next function in the array.
The execution of the next function in the array is prevented using the callbacks.disable()
function:
var sound = function( value ) { console.log( value ); }; var callbacks = $.Callbacks(); // Add the above function to the list callbacks.add( sound ); // Fire the items on the list callbacks.fire( "Woof!" ); // Disable further calls being possible callbacks.disable(); callbacks.fire( "Meow" );
3.142.255.140