$.when()方法翻译2
mac不知道为何,文章字数一多,浏览器就重启。只好分开写了。
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 );
In the multiple-Deferreds case where one of the Deferreds is rejected,jQuery.when()immediately fires the failCallbacks for its master Deferred. Note that some of the Deferreds may still be unresolved at that point. The arguments passed to the failCallbacks match the signature of the failCallback for the Deferred that was rejected. If you need to perform additional processing for this case, such as canceling any unfinished Ajax requests, you can keep references to the underlying jqXHR objects in a closure and inspect/cancel them in the failCallback.
Examples:
Execute a function after two Ajax requests are successful. (See the jQuery.ajax() documentation for a complete description of success and error cases for an ajax request).
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) {// a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.// Each argument is an array with the following structure: [ data, statusText, jqXHR ]var data = a1[ 0 ] + a2[ 0 ]; // a1[ 0 ] = "Whip", a2[ 0 ] = " It"if ( /Whip It/.test( data ) ) {alert( "We got what we came for!" );}});
Execute the functionmyFuncwhen both ajax requests are successful, ormyFailureif either one has an error.$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).then( myFunc, myFailure );
随机推荐
- PHP对象的复制
对象的复制(克隆) $obj1 = new A(); $obj1->p1 = 11; $obj2 = $obj1; //值传递 //则,现在有几个对象?——1个对象! 当然: $obj3 ...
- 第79天:jQuery事件总结(二)
上一篇讲到jQuery中的事件,深入学习了加载DOM和事件绑定的相关知识,这篇主要深入讨论jQuery事件中的合成事件.事件冒泡和事件移除等内容. 一.合成事件 jQuery有两个合成事件——hove ...
- MVC SignalR Hub初学
具体有关SignalR的说明查网上,我这里简单列举一个例子 1.新建MVC,添加SignalR引用(NuGet安装). 2.添加OWIN START类 public class Startup { p ...
- SQL Server Management Studio(SSMS)的使用与配置整理
目录 目录 SQL Server Management Studio的使用与配置 1 设置SSMS显示行号 2 添加注释与取消注释的快捷键 3 新建查询的快捷键 4 开启sql语句TIME与IO的统计 ...
- JavaScript常用方法(工具类的封装)
日期格式化 function formatDateTime(timeStamp) { var date = new Date(); date.setTime(timeStamp); var y = d ...
- Authenticator及AuthenticationStrategy
Authenticator的职责是验证用户帐号,是Shiro API中身份验证核心的入口点: 如果验证成功,将返回AuthenticationInfo 验证信息:此信息中包含了身份及凭证:如果验证失败 ...
- 洛谷 P4116 Qtree3
Qtree系列第三题 我是题面 读完题大概不难判断是一道树剖的题 这道题的关键是记录两种状态,以及黑点的序号(不是编号) 线段树啊当然 定义两个变量v,f,v表示距离根节点最近的黑点,默认-1,f则表 ...
- 【BZOJ5418】【NOI2018】屠龙勇士(数论,exgcd)
[NOI2018]屠龙勇士(数论,exgcd) 题面 洛谷 题解 考场上半个小时就会做了,一个小时就写完了.. 然后发现没过样例,结果大力调发现中间值爆\(longlong\)了,然后就没管了.. 然 ...
- 洛谷 P4721 【模板】分治 FFT 解题报告
P4721 [模板]分治 FFT 题目背景 也可用多项式求逆解决. 题目描述 给定长度为 \(n−1\) 的数组 \(g[1],g[2],\dots,g[n-1]\),求 \(f[0],f[1],\d ...
- HSTS的来龙去脉
前言 安全经常说“云.管.端”,“管”指的是管道,传输过程中的安全.为了确保信息在网络传输层的安全,现在很多网站都开启了HTTPS,也就是HTTP+TLS,在传输过程中对信息进行加密.HTTPS使用了 ...