$.when()方法翻译
地址: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 ThenableZero 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 asdeferred.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 byjQuery.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});
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"});
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 tojQuery.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 tojQuery.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" );
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 undefinedconsole.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 );
随机推荐
- 【Direct2D开发】 通过操作像素实现纹理混合
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 我们都知道Direct2D可以加载并显示图片,但是不知道你有没有想过,这个2D的图形引擎可以进行纹理混合吗?如果 ...
- JDBC基础学习(二)—PreparedStatement
一.PreparedStatement介绍 在SQL中包含特殊字符或SQL的关键字(如: ' or 1 or ')时Statement将出现不可预料的结果(出现异常或查询的结果不正确),可用P ...
- C# 遍历泛型集合
/// <summary> /// 遍历泛型 /// </summary> /// <typeparam name="T"></typep ...
- 用 js 的 selection range 操作选择区域内容和图片
原创文章,转载请注明出处并保留地址.原文地址:http://www.cnblogs.com/muge10/p/6723894.html 最近在做编辑器相关的东西,遇到一个需求,用户在编辑器中插入或者粘 ...
- HDFS中NameNode启动过程
移动到hadoop文件目录下 NameNode启动命令:sbin/hadoop-daemon.sh start namenode DataNode启动命令:sbin/hadoop-daemon.sh ...
- JDK源码之PriorityQueue源码剖析
除特别注明外,本站所有文章均为原创,转载请注明地址 一.优先队列的应用 优先队列在程序开发中屡见不鲜,比如操作系统在进行进程调度时一种可行的算法是使用优先队列,当一个新的进程被fork()出来后,首先 ...
- PMBOK 学习与实践分享视频
本系列为自己在学习PMBOK时进行的总结与分享,每一节主要包括两部分: 对PMBOK本身的一个结构笔记和讲解. 对自己项目管理工作的一个总结和思考. PMBOK 学习与实践分享视频内容清单 人力资源管 ...
- 【WPF】学习笔记(一)——做一个简单的电子签名板
参加实习(WPF)已经有两个多周的时间了,踩了一些坑,也算积累了一些小东西,准备慢慢拿出来分享一下.(●'◡'●) 这次呢就讲讲一个简单的电子签名板的实现. 先上张图(PS:字写得比较丑,不要太在意哈 ...
- Codeforces Round #410 (Div. 2)D题
D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Ajax第一课
<script language="javascript"></script> Javascript 函数创建 function 函数名(){ ...