deferred.done() JQ Home  <<  Objects  <<  deferred.done()

doneCallbacks additions.

Description

The deferred.done() Deferred method, adds handlers to be called when the Deferred object is resolved.


Syntax

Signature Description
deferred.done( doneCallbacks )Add handlers to be called when the Deferred object is resolved.

Parameters

Parameter Description Type
doneCallbacksA function(s), or array(s) of functions, that are called when the Deferred is resolved.Function

Return

A Deferred object.

deferred.done( doneCallbacks ) Example Objects  <<  Top

Add handlers to be called when the Deferred object is resolved.

In the example below when we press the left button the first time we create a Deferred object and use some deferred object methods on it. We then resolve the Deferred object and hence deferred.done( doneCallbacks ) gets called with the message passed from the resolve.

When we press the right button the first time we create a Deferred object and use some deferred object methods on it. We then reject the Deferred object and hence deferred.fail( failCallbacks ) gets called with the message passed from the reject.


$(function(){
  $('#btn5').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.done( cFunc );
    ourDeferred.fail( dFunc );
    ourDeferred.progress( cFunc, dFunc );
    ourDeferred.notify( 'function was fired from our deferred. <br>', '#div1');
    ourDeferred.resolve( 'Our deferred was resolved. <br> <br>', '#div1' );
  });
  $('#btn6').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.done( cFunc );
    ourDeferred.fail( dFunc );
    ourDeferred.progress( cFunc, dFunc );
    ourDeferred.notify( 'function was fired from our deferred. <br>', '#div1');
    ourDeferred.reject( 'Our deferred was rejected. <br> <br>', '#div1' );
  });
  function cFunc( value, div ){
    $(div).append( 'In cFunc: ' + value);
  }
  function dFunc( value, div ){
    $(div).append( 'In dFunc: ' + value);
  }
});

div1. Some initial text.

Press the button below to action the above code:

                

Related Tutorials

jQuery Advanced Tutorials - Lesson 7 - The Deferred Object