jQuery.Deferred()S2C Home « Objects « jQuery.Deferred()

Deferred object creation.

Shorthand version $.Deferred()

Description

The jQuery.Deferred() Deferred Constructor method, creates a Deferred object for managing callback queues.


Syntax


Signature Description
jQuery.Deferred( [initFunction] )Create a Deferred object for managing callback queues, optionally passing an initializing function.

Parameters


Parameter Description Type
initFunctionA function that is called just before the constructor returns and is passed the constructed Deferred object as both the this object and as the first argument to the function. The called function can then attach callbacks using any of the Deferred object's chainable methods.Function

Return

A Deferred object.

jQuery.Deferred( [initFunction] ) Examplesgo to top of page Top

Create a Deferred object for managing callback queues.

In the example below when we press the button the first time we create a Deferred object and use some deferred object methods on it.


$(function(){
  $('#btn1').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.always( aFunc );
    ourDeferred.progress( [aFunc, bFunc] );
    ourDeferred.notify( 'The aFunc function was fired from our deferred. <br />', '#div1');
    ourDeferred.resolve( 'Our deferred was resolved. <br /><br />', '#div1' );
  });
  function aFunc( value, div ){
    $(div).append( value);
  }
  function bFunc( value, div ){
    aFunc('Passing bFunc function value to aFunc. <br />', div);
  }
});

div1. Some initial text.

Press the button below to action the above code:



Create a Deferred object for managing callback queues passing an inititailizing function

In the example below when we press the button the first time we create a Deferred object using an initializing function which calls some deferred object methods on the newly created Deferred object passed to it.


$(function(){
  $('#btn2').one('click', function(){
    var ourDeferred = $.Deferred( initFunc );
  });
  function initFunc( newDeferredObj ){
    newDeferredObj.always( aFunc );
    newDeferredObj.progress( [aFunc, bFunc] );
    newDeferredObj.notify( 'aFunc function fired from deferred init function. <br />', '#div2');
    newDeferredObj.resolve( 'deferred was resolved in the init function. <br />', '#div2' );
  }
  function aFunc( value, div ){
    $(div).append( value);
  }
  function bFunc( value, div ){
    aFunc('Passing bFunc function value to aFunc. <br />', div);
  }
});

div2. Some initial text.

Press the button below to action the above code:



go to home page Homepage go to top of page Top