Callback

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().

callbacks.add()

This function is used to add all the functions in an array that are to be called later.

Parameters

This takes flags as strings as its parameters.

Returns

This method returns the callback's object to which it is associated with, (this).

Description

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!" );

callbacks.fire()

The callbacks.fire() function invokes the callbacks in a list with any arguments that have been passed.

Parameters

This takes a list of arguments to pass back to the callback list.

Returns

This returns the callback objects onto which it is attached.

Description

The callbacks.fire() function is used to invoke the callbacks in a list with the arguments. The preceding example could be referenced.

callbacks.remove()

The callbacks.remove() function is used to remove a function from the array.

Parameters

This takes flags as strings as its parameters.

Returns

This returns the callbacks object onto which it is attached.

Description

This removes the function from the callback array. The preceding example could be referenced.

callbacks.disable()

The callbacks.disable() function disables the call of the next function in the array.

Parameters

This does not take any parameter.

Returns

This returns the callbacks object to which the object is attached.

Description

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" );
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.255.140