$.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 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 );
随机推荐
- webpack搭建服务器,随时修改刷新
前提:1.对webpack有一定了解,本文不做介绍 2.安装node.js 手把手操作: 1.创建一个名为webpack-server的文件夹(随便取的) 2.cd到当前文件夹:cd webpack- ...
- Why we don’t recommend using List<T> in public APIs
不推荐List<T>做API原因有如下两点:1.首先List<T> 设计之初就没有设计成可扩展的,我们不能重新其任何方法.这就意味着,我们操作List<T>的时候却 ...
- 过滤器Filter(17/4/8)
1:是JavaWeb三大组件之一: Servlet.Lisener(2个感知监听器不需要配置).Filter 2:过滤器 它会在一组资源(jsp.servlet.css.html等等)的前面执行! 它 ...
- jsp实现仿QQ空间新建多个相册名称,向相册中添加照片
工具:Eclipse,Oracle,smartupload.jar:语言:jsp,Java:数据存储:Oracle. 实现功能介绍: 主要是新建相册,可以建多个相册,在相册中添加多张照片,删除照片,删 ...
- iOS·UIKit & Foundation框架—Annotations & Category注解工具类
- 如何做一个导航栏————浮动跟伪类(hover)事件的应用
我们先说一下伪类选择器的写法: 写法:选择器名称:伪类状态{}4 常见伪类状态: 未访问:link 鼠标移上去:hover 激活选定:active 已访问:visited 获得焦点的时候触发:focu ...
- JavaScript中var变量引用function与直接声明function
今天在h5开发app的过程中遇到了一个js问题,function的执行问题 在js中声明函数function有这两种方法 var A=function(){...} 或者 function A(){. ...
- C++随机数rand(), srand()
c++产生随机数会用到rand(), srand()函数,下面总结两个函数特性和使用. 1. rand() #include <iostream> #include <cstdlib ...
- 蓝桥杯-加法变乘法-java
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...
- javascript执行原理
执行环境 当执行流执行到函数时会创建一个执行环境,这个执行环境包含了函数内部 语句可以访问的所有变量和函数,当代码执行完时,销毁执行环境.所以一般情 况下,局部变量在函数执行完时会被销毁. 作用域.调 ...