jQuery deferred.done() Method
Example
When a user clicks the button, resolve the deferred object, triggering a series of callback functions.
<button>Go</button>
<p>Ready...</p>
<script>
$(function () {
// 3 functions are called when the Deferred object is resolved
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 to be called when dfd is resolved
dfd
// .done() can take functions or arrays of functions
.done( [ fn1, fn2 ], fn3, [ fn2, fn1 ] )
// We can also chain other methods
.done(function( n ) {
$( "p" ).append( n + " Completed." );
});
// Change the status to resolved when the button is clicked
$( "button" ).on( "click", function() {
dfd.resolve( "and" );
});
})
</script>
Definition and Usage
The deferred.done() function adds handlers to be called when the Deferred object is resolved. Note: This method accepts one or more arguments. deferred.done() returns the Deferred object, allowing for the chaining of other Deferred methods, including additional .done() methods. When the Deferred object is resolved, the callback functions are executed in the order they were added and can be passed as arguments to methods such as resolve, resolveWith.
Syntax
| Parameter | Description |
|---|---|
| doneCallbacks | Function type. A function or array of functions to be called when the Deferred object is resolved. |
| doneCallbacks | Optional. Function type. A function or array of functions to be called when the Deferred object is resolved. |