jQuery deferred.done()Method
Example
When the user clicks the button, accept the deferred object and trigger a series of callback functions
<button>Go</button>
<p>Preparing...</p>
<script>
$(function () {
//When the Deferred object is resolved, 3 functions are called
function fn1() {
$( "p" ).append( " 1 " );
}
function fn2() {
$( "p" ).append( " 2 " );
}
function fn3( n ) {
$( "p" ).append( n + " 3 " + n );
}
//Create a deferred object
var dfd = $.Deferred();
//Add handlers that are called when dfd is resolved
dfd
//.done() can take a function or an array of functions
.done( [ fn1, fn2 ], fn3, [ fn2, fn1 ] )
//We can also chain other methods
.done(function( n ) {
$( "p" ).append( n + "Completed." );
});
//When the button is clicked, change the state to completed
$( "button" ).on( "click", function() {
dfd.resolve( "and" );
});
})
</script>
Try it »
Definition and Usage
deferred.done() calls the added handlers when the Deferred object is resolved.
Tip:This method accepts one or more parameters. deferred.done() returns a Deferred object, which can chain other deferred object methods, including additional .done() methods. When the Deferred object is resolved, the callback functions execute in the order they were added, and can be passed as arguments to methods such as: resolve, resolveWith.
Syntax
deferred.done( doneCallbacks [, doneCallbacks ] )
| Parameter | Description |
|---|---|
| doneCallbacks | Function type: A function or an array of functions called when the Deferred object is resolved |
| doneCallbacks | Optional. Function type: A function or an array of functions called when the Deferred object is resolved |
Other extensions
jQuery Miscellaneous Methods