$.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 );
随机推荐
- shell脚本编写步骤及其常用命令和符号
1,什么是Shell Shell 是kernel的一个外壳,是一个命令解析器,负责用户与内核的交互.2,Shell脚本 Shell脚本类似于批处理,可以方便的执行大量命令.3,编写sh ...
- js的apply()与call()的区别
1.各自对应的不同的语法: /*apply()方法*/ function.apply(thisObj[, argArray]) /*call()方法*/ function.call(thisObj[, ...
- Spring Boot 学习笔记--整合Thymeleaf
1.新建Spring Boot项目 添加spring-boot-starter-thymeleaf依赖 <dependency> <groupId>org.springfram ...
- bigdecimal更精确的浮点处理方式
Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算.双精度浮点型变量double可以处理16位内有效数,超过16位,double可能会出现内存 ...
- Java多线程学习笔记(一)——Thread类中方法介绍
currentThread():返回代码正在被哪个线程调用. public class CurrentThreadWay { public static void main(String[] args ...
- C#网络程序设计(1)网络编程常识与C#常用特性
网络程序设计能够帮我们了解联网应用的底层通信原理! (1)网络编程常识: 1)什么是网络编程 只有主要实现进程(线程)相互通信和基本的网络应用原理性(协议)功能的程序,才能算是真正的网 ...
- stm32中的延时函数
//粗延时函数,微秒 void delay_nus(u16 time) { u16 i=0; while(time--) { i=10; //自己定义 while(i--) ; } } //毫秒级的 ...
- Python HTMLTestRunner生成网页自动化测试报告时中文编码报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6
1. 由于使用Python Selenium做网页自动化测试时,有截取网页上的中文信息保存到测试结果中,最终出现编码错误如下: File "D:/PycharmProjects/AutoTe ...
- Java匿名内部类使用与示例
首先说为什么有匿名类 两个原因(产生的使命) 1.简化代码编写 某种情况下,类只需要扩展一个方法,没必要为了一个方法单独去写一个子类,然后然后调用子类,此时需要匿名类 2.在不同的包内的类内调用类的p ...
- Python全栈之路-Day32
1 类的__slots__ #!/usr/bin/env python # __Author__: "wanyongzhen" # Date: 2017/4/25 # 只能定义__ ...