地址:http://api.jquery.com/jQuery.when/

jQuery.when( deferreds ),returns Promise

正文

Description: Provides a way to execute callback functions based on zero or more Thenable objects, usually Deferred objects that represent asynchronous events.

描述:提供一个执行回调函数的方法,它是基于0或多个继发对象的,这些对象通常是表示异步事件的deferred对象。

deferreds

Type: Deferred or Promise or Thenable
Zero or more Thenable objects.

jQuery.when( deferreds )方法是 JQ1.5版本后添加的方法。传入的参数deferreds类型为deferred,promise,thenable,0或多个继发对象。

If no arguments are passed to jQuery.when(), it will return a resolved Promise.

如果不传递参数给when,该方法将返回一个已完成状态的promise。关于Promise(参看:http://www.jianshu.com/p/063f7e490e9a)。

If a single Deferred is passed to jQuery.when(), its Promise object (a subset of the Deferred methods) is returned by the method. Additional methods of the Promise object can be called to attach callbacks, such as deferred.then. When the Deferred is resolved or rejected, usually by the code that created the Deferred originally, the appropriate callbacks will be called. For example, the jqXHR object returned by jQuery.ajax() is a Promise-compatible object and can be used this way:

$.when( $.ajax( "test.aspx" ) ).then(function( data, textStatus, jqXHR ) {
alert( jqXHR.status ); // Alerts 200
});
如果仅将一个deferred传给jQuery.when(),一个Promise对象(deferred对象的子集)将被这个方法返回。promise对象的附加方法将可以附着到回调上被调用。跟deferred一样。通常一个deferred被创建它的原始脚本完成或者失败后,对应的回调将被调用。比如,被jQuery.ajax()返回的jqXHR对象是一个混合的promise对象,可以如下使用:例子见上,不赘述。
 

If a single argument is passed to jQuery.when() and it is not a Deferred or a Promise, it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately. The doneCallbacks are passed the original argument. In this case any failCallbacks you might set are never called since the Deferred is never rejected. For example:

$.when( { testing: 123 } ).done(function( x ) {
alert( x.testing ); // Alerts "123"
});
 如果仅将一个参数传入jQuery.when(),并且它不是一个deferred或者promise对象,它将被当作一个已完成的deferred对象,任何done附着的done回调将立即被执行。done回调将被传入原始参数。这样的话,任何的fail回调都不能设置或者调用,因为deferred永远不会失败。举例如下:例子见引用。
 

If you don't pass it any arguments at all, jQuery.when() will return a resolved promise.

$.when().then(function( x ) {
alert( "I fired immediately" );
});
如果不传递任何参数,jQuery.when()将返回一个完成状态的promise。
 
In the case where multiple Deferred objects are passed to jQuery.when(), the method returns the Promise from a new "master" Deferred object that tracks the aggregate state of all the Deferreds it has been passed. The method will resolve its master Deferred as soon as all the Deferreds resolve, or reject the master Deferred as soon as one of the Deferreds is rejected. If the master Deferred is resolved, the doneCallbacks for the master Deferred are executed. The arguments passed to the doneCallbacks provide the resolved values for each of the Deferreds, and matches the order the Deferreds were passed to jQuery.when(). For example:
var d1 = $.Deferred();
var d2 = $.Deferred();
 
$.when( d1, d2 ).done(function ( v1, v2 ) {
console.log( v1 ); // "Fish"
console.log( v2 ); // "Pizza"
});
 
d1.resolve( "Fish" );
d2.resolve( "Pizza" );
既然这样,如果传递复合的deferred对象给jQuery.when(),方法将返回一个promise对象,它来自于一个新的主deferred对象,这个对象会跟踪记录下所有传入的deferreds的全部轨迹。这个方法在所有defferreds完成后将立即完成它的主deferred,或者会在所有deferreds失败的时候后立即置为失败。如果主deferred已经完成,它的done回调会被执行。传入到done回调的所有参数提供了所有deferreds的完成值,并且按照传递给jQuery.when()的所有deferred的顺序,例如:例子见上。
再举一个例子:
var dtd = $.Deferred();
var wait=function(dtd){
setTimeout(task, 8000);
function task(){
console.log("i'm jiangtian!");
dtd.resolve();
}
return dtd;
} $.when(wait(dtd)).done(function(){
console.log("累死我了,终于执行完了。");
})

阮一峰这篇文章写的相当详细,推荐:《jQuery的deferred对象详解 - 阮一峰的网络日志

In the event a Deferred was resolved with no value, the corresponding doneCallback argument will be undefined. If a Deferred resolved to a single value, the corresponding argument will hold that value. In the case where a Deferred resolved to multiple values, the corresponding argument will be an array of those values. For example:

var d1 = $.Deferred();
var d2 = $.Deferred();
var d3 = $.Deferred();
$.when( d1, d2, d3 ).done(function ( v1, v2, v3 ) {
console.log( v1 ); // v1 is undefined
console.log( v2 ); // v2 is "abc"
console.log( v3 ); // v3 is an array [ 1, 2, 3, 4, 5 ]
});
d1.resolve();
d2.resolve( "abc" );
d3.resolve( 1, 2, 3, 4, 5 );
 

随机推荐

  1. HTTP 协议

    HTTP 协议对应 Web 开发者来说都必须要了解的,无论技术背景或首选编程语言是什么,"请求-响应" 对话是驱动 Web 上通信的基础. HTTP 概述 HTTP 协议是 Hyp ...

  2. 使用validator-api来验证spring-boot的参数

    作为服务端开发,验证前端传入的参数的合法性是一个必不可少的步骤,但是验证参数是一个基本上是一个体力活,而且冗余代码繁多,也影响代码的可阅读性,所以有没有一个比较优雅的方式来解决这个问题? 这么简单的问 ...

  3. "fatal: protocol error: bad line length character: No This"

    git clone 远程地址时候出现 "fatal: protocol error: bad line length character: No This" 错误 在stackov ...

  4. 厉害了我的雅虎!卖掉主业后更名为阿里他爸(Altaba)

    据雅虎周一向美国证券交易委员会(SEC)提交的文件显示,在美国通信巨头Verizon斥资48亿美元收购雅虎的交易完成后,该公司首席执行官玛丽莎o梅耶尔(Marissa Mayer)将退出公司董事会. ...

  5. Laravel 5.2 教程 - 数据填充

    一.简介 Laravel提供的填充类(seed),可以让大家很容易的实现填充测试数据到数据库.所有的填充类都位于database/seeds目录.填充类的类名完全由你自定义,但最好还是遵循一定的规则, ...

  6. smart beta

    本文来至人大经济论坛,http://bbs.pinggu.org/thread-3151691-1-1.html 众所周知,beta在CAPM模型中衡量了相对于持有整个市场所带来的风险溢价(risk ...

  7. Spark机器学习之协同过滤算法

    Spark机器学习之协同过滤算法 一).协同过滤 1.1 概念 协同过滤是一种借助"集体计算"的途径.它利用大量已有的用户偏好来估计用户对其未接触过的物品的喜好程度.其内在思想是相 ...

  8. 使用OpenOffice实现各种文档转pdf或者html文档

    ---恢复内容开始--- 最近在做项目时需要写一个功能,将doc,ppt,xsl等文档做在线预览.网上查了很多资料,开始适用poi将文档转成pdf没成功,后来使用了OpenOffice4 + jodc ...

  9. vue2.0 组件通信

    组件通信: 子组件要想拿到父组件数据 props 子组件不允许直接给父级的数据, 赋值操作如果想更改,父组件每次穿一个对象给子组件, 对象之间引用. 例子: <script> window ...

  10. OSS.Core基于Dapper封装(表达式解析+Emit)仓储层的构思及实现

    最近趁着不忙,在构思一个搭建一个开源的完整项目,至于原因以及整个项目框架后边文章我再说明.既然要起一个完整的项目,那么数据仓储访问就必不可少,这篇文章我主要介绍这个新项目(OSS.Core)中我对仓储 ...