jQuery deferred.done() method
Example
When the user clicks the button, resolve the deferred object, triggering a series of callback functions
<
button
Go
button
<
pReady...
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 a handler to be called when dfd is resolved
dfd
//
.done() can take any number of 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 completed when the button is clicked
$
(
"
button
"
)
.
on
(
"
click
"
,
function
(
)
{
dfd
.
resolve
(
"
and
"
)
;
}
)
;
}
)
script
Try it »
Definition and Usage
The deferred.done() function calls the added handlers when the Deferred object is resolved.
**Tip:** This method accepts one or more arguments. deferred.done() returns a Deferred object, so other deferred object methods can be chained to it, including additional .done() methods. When the Deferred object is resolved, the callbacks are executed in the order in which they were added, and can be passed as arguments to methods such as: resolve, resolveWith.
Syntax
deferred.done( doneCallbacks [, doneCallbacks ] )
Parameters 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
jQuery Miscellaneous Methods