jQuery.Deferred()
JQ 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.
- Only the creator of a Deferred object should call the
deferred.notify()
,deferred.notifyWith()
,deferred.resolve()
,deferred.resolveWith()
,deferred.reject()
ordeferred.rejectWith()
methods. Other code can be stopped from changing a Deferred objects state or reporting status by returning a restricted Promise object through thedeferred.promise()
method. - After creating a Deferred object, you can use any of the Deferred object methods by chaining directly on object creation, or saving the object to a variable and then invoking the Deferred object methods on that variable.
Syntax
Signature | Description |
---|---|
jQuery.Deferred( [initFunction] ) | Create a Deferred object for managing callback queues, optionally passing an initializing function. |
Parameters
Parameter | Description | Type |
---|---|---|
initFunction | A 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] )
ExamplesObjects << 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.
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.
Related Tutorials
jQuery Advanced Tutorials - Lesson 7 - The Deferred Object