$.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 );
随机推荐
- 安卓 ADB常见问题整理
以下都是ADB连接问题,可以通过尝试如下步骤,由简单度排序 1. 插拔下USB连接线 2. 关闭USB模式再打开 3. 执行以下命令 adb kill-server adb start-server ...
- XML文档结构
<?xml version="1.0" encoding="UTF-8"?> <books> <bool id="bk1 ...
- npm安装
淘宝镜像http://npm.taobao.org/ $ npm install -g cnpm --registry=https://registry.npm.taobao.org mac下 sud ...
- PAT 1046
1046. Shortest Distance (20) The task is really simple: given N exits on a highway which forms a sim ...
- Linux-粘滞位的使用
粘滞位(Stickybit),又称粘着位,是Unix文件系统权限的一个旗标.最常见的用法在目录上设置粘滞位, 也只能针对⽬录设置,对于⽂件⽆效.则设置了粘滞位后,只有目录内文件的所有者或者root才可 ...
- AngularJS的过滤器$filter
过滤器(filter)主要用于数据的格式上,通过某个规则,把值处理后返回结果.例如获得数据集,可排序后再返回. ng内置的共有九种过滤器: currency 货币 使用currency可以将数字格式化 ...
- Vue 自定义图片懒加载指令v-lazyload
Vue是可以自定义指令的,最近学习过程中遇见了一个需要图片懒加载的功能,最后参考了别人的代码和思路自己重新写了一遍.以下将详细介绍如何实现自定义指令v-lazyload. 先看如何使用这个指令: &l ...
- HNOI2017 滚粗记
这次HNOI,感觉自己收获了很多啊,高一的蒟蒻,也就是去历练一番,长长见识吧.. $day0$ 上午做了一道斜率优化的题,下午好像在颓??晚上也不想复习了,看了会电视,$12$点才睡.. $day1$ ...
- Nginx 常用配置整理
#定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | ...
- <图论入门>邻接矩阵+邻接表
非本人允许请勿转载. 趁热打铁,学会了邻接表把这个总结一下,以及感谢大佬uncle-lu!!!(奶一波)祝早日进队! 首先,图论入门就得是非常基础的东西,先考虑怎么把这个图读进去. 给定一个无向图,如 ...