The jquery deferred object is "Deferred", which is a linkable utility object created by calling the jQuery.Deferred() method. A deferred object is chainable, similar to the way a jQuery object is chainable, except that it has its own methods; it can register multiple callback functions to a callback list, call the callback list and pass the success or failure of an asynchronous or synchronous function state.
The operating environment of this tutorial: windows7 system, jquery3.6.1 version, Dell G3 computer.
Introduced in jQuery 1.5, Deferred objects are linkable utility objects created by calling the jQuery.Deferred() method. It can register multiple callback functions to the callback list, call the callback list and pass the success or failure status of asynchronous or synchronous functions.
A deferred object is chainable, similar to the way a jQuery object is chainable, except that it has its own methods. After creating a Deferred object, you can use any of the following methods to link directly to the object that was created or saved by calling one or more of its methods.
$.Deferred()
$.Deferred() is a constructor, used To return a chained utility object method to register multiple callbacks and call the callback queue, passing the success or failure status of any synchronous or asynchronous functions.
Tips:
doneCallbacks immediately after transitioning the delay to the resolved state.
Executes the set failCallbacks immediately after calling deferred.reject() or deferred.rejectWith() to convert the delay into a reject state. Once an object has entered the resolved or rejected state, it remains in that state. Callbacks can still be added to resolved or rejected Deferred objects - they are executed immediately.
Parameter description
Function | Description |
---|---|
$.Deferred() | is used to return a chained utility object method to register multiple callbacks, and call the callback queue to pass the success or failure status of any synchronous or asynchronous function. |
deferred.always() | Used to call the added handler when the Deferred object is accepted or rejected |
deferred.done() | Used to call the added handler when the Deferred (deferred) object is accepted |
deferred.fail() | Used to call the added handler when the Deferred (deferred) object is rejected |
deferred.progress() | Used when the Deferred (Deferred) When the object generates a progress notification, call the add handler |
deferred.isRejected()1.8- | Used to determine whether the Deferred object has been rejected |
##deferred.isResolved()1.8- | |
is used to pass parameters and the context object to the ongoing callback | |
is used to reject the delayed object , and pass parameters to the failure callback function | |
is used to reject the delay object, and pass parameters and context objects to the failure callback function | |
is used to resolve the deferred object and pass parameters to the doneCallbacks callback function | |
is used to resolve the deferred object and pass parameters and context objects to doneCallbacks callback function | |
is used to determine The current state of a Deferred object | |
Used to filter the state or the deferred object returned by the function The value | |
Description | |
---|---|
A function called before the constructor returns |
deferred.always()<span style="font-size: 18px;"></span>When the Deferred (deferred) object is accepted or rejected, call through deferred.always () added handler.
Tip: The parameter can be a function or an array of functions. Since deferred.always() returns a Deferred object, it can be connected to other deferred object methods (
Here refers to methods that can continue to call Deferred objects), including additional .always methods. When a Deferred object is resolved or rejected, callback functions added via deferred.always() are executed in the order in which they were added, and can be passed as arguments to the following methods: resolve , reject , resolveWith or rejectWith . Syntax
ParameterDescriptionalwaysCallbacks A function or array of functions that is called when the Deferred object is resolved or rejected Note: The deferred.always() method receives the parameters used by .resolve() or .reject() of the Deferred object, which are often very different. For this reason, it is best to just use its behavior without checking its parameters. In most cases, use explicit .done() or .fail() handlers because their parameters are known.Parameter description
Optional/Function type |
1 2 3 |
|
deferred .done()<span style="font-size: 18px;"></span>
When the Deferred (deferred) object is accepted, the handler added by the delayed object function deferred.done()
Tips: This method accepts one or more parameters. deferred.done() returns a Deferred object that can be connected to other deferred object methods (continue to call the methods of the Deferred object), including additional .done() methods. When the Deferred object is resolved, the callback functions added through deferred.done() are executed in the order in which they were added, and can be passed as parameters to the following methods: resolve, resolveWith.
ParameterDescriptiondoneCallbacks A function or array of functions that is called when the Deferred object is resolvedParameter description
deferred.done() 返回的是一个 Deferred 对象
HTML示例代码:
1 2 |
|
jQuery示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
<span style="font-size: 18px;">deferred.fail()</span>
当 Deferred (延迟)对象被拒绝时,调用通过deferred.fail()添加的处理程序。
提示:该方法接受一个或者多个参数。 deferred.fail() 返回的是一个 Deferred 对象, 可以连接其他的延迟对象方法(继续调用其他Deferred 对象的方法),包括额外的 .fail() 方法。当 Deferred 对象被拒绝时,通过deferred.fail()添加的回调函数 按它们被添加时的顺序执行,并且可以作为参数传递给如下的方法使用:deferred.resolve() 或 deferred.rejectWith()。
jQuery1.5新增该函数
Optional/Function type |
参数 | 说明 |
---|---|
failCallbacks | 可选/Function类型 一个函数或者函数数组,当Deferred(延迟)对象被拒绝时被调用 |
deferred.fail() 返回的是一个 Deferred 对象
jQuery示例代码
1 2 3 4 5 |
|
<span style="font-size: 18px;">deferred.progress()</span>
deferred.progress() 函数当Deferred(延迟)对象生成进度通知时,调用添加处理程序。
注意:当通过调用 notify 或 notifyWith 使延迟对象产生进度通知时,progressCallbacks 就会被调用。 由于 deferred.progress()返回的是延迟对象,所以其它延迟对象方法可以链接到该对象上(链式调用)。当延迟对象被 resolved 或 rejected 时, 进度回调函数将不再被调用,但是当Deferred (延迟)进入resolved(解决) 或 rejected(拒绝)状态后,新添加任何的progressCallbacks将立即执行,使用的参数被传递给.notify() 或 notifyWith()调用
jQuery 1.7 新增该函数
参数 | 说明 |
---|---|
progressCallbacks | 可选/Function类型 一个函数或者函数数组,当Deferred(延迟)对象生成正在执行中的进度通知时被调用。 |
deferred.progress() returns a Deferred object
##deferred.isRejected( )<span style="font-size: 18px;"></span>
Deprecated as of jQuery 1.7, please use deferred.state() instead.Note:
This function was added in jQuery 1.5, obsolete in 1.7, and removed in 1.8Return valuedeferred.isRejected() returns a
Boolean type
deferred.isResolved()<span style="font-size: 18px;"></span>
Deprecated as of jQuery 1.7, please use deferred.state() instead.Note:
This function was added in jQuery 1.5, obsolete in 1.7, and removed in 1.8Return valuedeferred.isResolved() returns a
Boolean type
deferred.notify()<span style="font-size: 18px;"></span>
Pass parameters from .notify() to the callback function on the calling delay object, and then call any after the delay object has been resolved or rejected. notify() (or add progressCallbacks) will <br/> be ignored<br/>
jQuery 1.7 Added this functionParameter description##ParameterargsOptional/Object type
deferred.notify() 返回的是一个 Deferred 对象
jQuery提供的deferred.promise()方法的作用是,在原来的Deferred 对象上返回另一个 Deferred 对象,即受限制的 Promise 对象,受限制的 Promise 对象只开放与改变执行状态无关的方法(比如done()方法和fail()方法),屏蔽与改变执行状态有关的方法(比如resolve()方法和reject()方法),从而使得执行状态不能被改变。
首先看一个 Deferred对象的执行状态被改变的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
再看一个 Deferred对象返回deferred.promise()的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
wait()函数返回的是promise对象。dtd.promise().resolve()方法不存在,因此无法改变状态
,然后,我们把回调函数绑定在这个对象上面,而不是原来的deferred对象上面。
这样的好处是,无法改变promise对象的执行状态,要想改变执行状态,只能操作原来的deferred对象。
<span style="font-size: 18px;">deferred.notifyWith()</span>
deferred.notifyWith() 函数用于给定上下文和参数,传递给正在调用的延迟对象上进行的回调函数( progressCallbacks )。
注意:
通过 .notifyWith() 传递参数给每个回调函数,当迟延对象已经被 resolved 或被 <br/> rejected 之后,再调用任何 .notifyWith() (或者添加 progressCallbacks) 都会被忽略。
jQuery 1.7 新增该函数
Description | |
---|---|
Pass an optional parameter to the ongoing callback (progressCallbacks) |
参数 | 说明 |
---|---|
context | Object类型 作为this对象,传递给进行中的回调(progressCallbacks) |
args | Array类型 传递一个可选的参数给进行中的回调(progressCallbacks) |
deferred.notifyWith() 返回的是一个 Deferred 对象
<span style="font-size: 18px;">deferred.reject()</span>
deferred.reject() 函数用于拒绝延迟对象,并根据给定的参数调用任何 failCallbacks 回调函数。
注意:
jQuery1.5新增该函数
参数 | 说明 |
---|---|
args | Object类型 传递一个可选的参数给失败的回调(failCallbacks) |
deferred.reject() returns a Deferred object
<span style="font-size: 18px;">deferred.rejectWith( )</span>
deferred.rejectWith() function is used to reject the deferred object and call any failCallbacks callback function based on the given context and args parameters.
Note:
##jQuery1.5 added this functionParameter descriptionParameterDescription contextargs
Object type Pass an object to failure callbacks (failCallbacks) | |
Array type Pass an optional parameter to the failed callback (failCallbacks) |
Deferred object
deferred.resolve()<span style="font-size: 18px;"></span>
jQuery1.5 added this functionParameter descriptionParameterDescriptionargs
Object type Pass an optional parameter to the completion callback function (doneCallbacks) |
Deferred object
##deferred.resolveWith()<span style="font-size: 18px;"></span>
# The ##deferred.resolveWith() function is used to resolve the Deferred (deferred) object and complete the callback function doneCallbacks based on the given context and args parameters.
Usually only the creator of the deferred object can call this method. You can prevent other code from changing the state of the deferred object or reporting its state by calling deferred.promise(), which returns a restricted Promise object.
ParameterDescription context Pass the context object to the completion callback function (doneCallbacks)args Pass an optional parameter to the completion callback function (doneCallbacks)Parameter description
deferred.resolveWith() returns a Deferred object
<span style="font-size: 18px;">deferred.state( )</span>
deferred.state() function is used to determine the current state of a Deferred object.
Mainly used for debugging, for example, before preparing to reject a deferred object, determine whether it is already in the resolved state.
Note:
The deferred.state() method returns a string representing the current state of the Deferred object. Deferred objects can be in one of three states:
jQuery1.7 added this function
deferred.state() returns a String type
<span style="font-size: 18px;">deferred.pipe() </span>
#deferred.pipe() function is a utility method for filtering and/or chaining deferred objects.
Note: Starting from jQuery 1.8, the deferred.pipe() method is obsolete
. Use deferred.then() instead.
As of jQuery 1.8, the deferred.pipe() method is obsolete. deferred.then() should be used instead.
The deferred.pipe() method returns a new Promise object, is used to filter the status or the value of the deferred object returned by the function
, and is passed to the corresponding Promise object The done() and fail() methods
doneFilter and failFilter functions filter the status and value of the original deferred (deferred) resolution/rejection.
Starting from jQuery 1.7, this method also accepts a progressFilter function to filter any notify or notifyWith methods that access the deferred (deferred).
These filters can return a new value when the done() or fail() callback function of the promise object returned by pipe() is called, or return other visible objects (Deferred, Promise, etc.) , these visible objects pass
their own resolve/reject status
and the return value
passed to the pipe promise callback function.
If null is used as the filter function, or no filter function is specified, the same value will be used as the original value when the pipe promise is accepted (resolve) or rejected (reject).
## This function was added in jQuery 1.6 and is obsolete in 1.8
This function was added in jQuery 1.7 and is obsolete in 1.8Parameter description
Object type | |
Array type |
deferred.pipe() 返回的是一个 * Promise对象*
过滤解决值:
1 2 3 4 5 6 7 8 9 |
|
过滤拒绝值:
1 2 3 4 5 6 7 8 9 10 11 |
|
链任务(?)
:
1 2 3 4 5 6 7 8 9 |
|
<span style="font-size: 18px;">deferred.then()</span>
deferred.then() 函数当Deferred(延迟)对象被解决/拒绝或仍在进行中时,调用添加处理程序。
注意:
doneFilter 和 failFilter函数过滤原延迟对象的解决/拒绝的状态和值。
progressFilter 函数过滤任何调用原有的延迟对象的notify 和 notifyWith的方法。
这些过滤器函数可以返回一个新的值传递给的 Promise 对象的.done() 或 .fail() 回调,或他们可以返回另一个观察的对象(延迟对象,Promise 对象等)传递给它的解决/拒绝的状态和值,Promise 对象的回调。
如果过滤函数是空,或没有指定,promise(承诺)将得到与原来值相同解决(resolved)或拒绝(rejected)。
deferred.then( doneFilter [, failFilter ] [, progressFilter ] )
jQuery1.8新增该函数
deferred.then1.8-( doneCallbacks, failCallbacks)
jQuery1.5新增该函数,1.8移除
deferred.then1.8-( doneCallbacks, failCallbacks[, progressFilter ] )
jQuery1.7新增该函数,1.8移除
Function type Optional function that is called when the delay is resolved | |
Function Type Optional function, called when delay is rejected | |
Function Type Optional function, called when progress notifications are sent to Deferred(deferred) is called |
参数 | 说明 |
---|---|
doneFilter | Function类型 可选 当Deferred(延迟)对象得到解决时被调用的一个函数 |
failFilter | Function类型 可选 当Deferred(延迟)对象得到拒绝时被调用的一个函数 |
progressFilter | Function类型 可选 当Deferred(延迟)对象生成进度通知时被调用的一个函数调用 |
doneCallbacks | Function类型 当Deferred(延迟)对象得到解决时被调用的一个函数或函数数组 |
failCallbacks | Function类型 当Deferred(延迟)对象得到拒绝时被调用的一个函数或函数数组 |
progressCallbacks | Function类型 当Deferred(延迟)对象生成进度通知时被调用的一个函数或函数数组 |
deferred.then() 返回 Promise 对象
HTML代码:
1 2 |
|
过滤解决值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
过滤拒绝值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
链任务(?)
:
1 2 3 4 5 6 7 8 9 |
|
<span style="font-size: 18px;">deferred.catch()</span>
当Deferred对象被拒绝(reject)时,调用通过deferred.catch()添加的处理程序。
deferred.catch( fn ) 是 deferred.then( null, fn )的一个别名
jQuery 3.0 新增该函数
参数 | 说明 |
---|---|
failCallbacks | Function类型 一个函数,当 Deferred 对象被拒绝(reject)时被调用 |
deferred.catch() 返回的是一个 Promise 对象
jQuery.get 方法返回一个jqXHR对象, 它是从Deferred对象派生的,当Deferred对象被拒绝(reject) 时,我们可以使用.catch方法来处理,jQuery示例代码:
1 2 3 4 5 6 7 |
|
<span style="font-size: 18px;">deferred.promise()</span>
deferred.promise() 函数返回 Deferred(延迟)的 Promise 对象。
注意:
方法允许一个异步函数阻止那些干涉其内部请求的进度(progress)或状态(status)的其它代码。
Promise 对象只包含 deferred 对象的一组方法,包括:done(),then(),fail(),isResolved(), isRejected(), always(), 这些方法只能观察一个 deferred 的状态;不包括任何用于改变状态的延迟方法(resolve, reject, notify, resolveWith, rejectWith, 和 notifyWith),因此Promise 对象无法更改 deferred 对象的内在状态
jQuery 1.5 新增该函数
参数 | 说明 |
---|---|
target | Object类型 绑定 promise 方法的对象。 |
deferred.promise() 返回的是一个 Promise 对象
创建一个延迟对象,并设定两个延时时间是随机的定时器,分别用于受理(resolve)和拒绝(reject)延迟对象。无论哪一个先执行,都会调用其中一个回调函数。而另一个定时器则不会产生任何效果,因为在最先调用的那个定时器处理中,延迟对象已经处于完成状态(resolved 或 rejected 状态)。同时,还会设定一个定时器进度(progress)通知函数,用于进度通知处理,并在文档的 “body” 中显示 “working…”,以下为一次测试时的jQuery示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
使用目标参数,产生现有对象的Promise对象:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
<span style="font-size: 18px;">.promise()</span>
.promise() 函数返回一个 Promise 对象,观察某种类型被绑定到集合的所有行动,是否已被加入到队列中。
返回的 Promise 被链接到延迟对象上,保存在元素的 .data() 中。由于 .remove() 方法会移除元素上的 data,同时也会移除元素本身。所以,使用它会防止任何元素上未被受理的(unresolved) Promise 被受理(resolving)。如果有必要在元素的 Promise 被受理(resolved)之前,从 DOM 中移除该元素的话,请使用.detach() 来代替。之后再调用 .removeData()
注意:
jQuery 1.5新增
参数 | 说明 |
---|---|
type | 可选/String类型 需要待观察队列类型。 |
target | 可选/PlainObject类型 将要绑定 promise 方法的对象。 |
默认情况下, type的值是”fx” ,这意味着返回被受理(resolve)的 Promise 对象的时机,是在所有被选中元素的动画都完成时发生的。
如果提供target参数,.promise()在该参数上添加方法,然后返回这个对象,而不是创建一个新的。它适用于在一个已经存在的对象上添加 Promise 行为的情况。
.promise()方法返回一个动态生成的 Promise对象。
1.在一个没有激活动画的集合上调用 .promise()
相关的jQuery示例代码:
1 2 3 4 5 6 |
|
2.当所有的动画结果时(包括那些在动画回调函数和之后添加的回调函数中初始化的动画),受理(Resolve)返回的 Promise,相关HTML代码
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
当所有的动画结果时(包括那些在动画回调函数和之后添加的回调函数中初始化的动画),受理(Resolve)返回的 Promise,相关jQuery代码
:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
使用 $.when() 语句(.promise() 方法使得在 jQuery 集合中实现它变成了可能),受理(Resolve)返回的 Promise
1 2 3 4 5 6 7 8 9 10 |
|
【推荐学习:jQuery视频教程、web前端视频】
The above is the detailed content of What is jquery deferred object. For more information, please follow other related articles on the PHP Chinese website!