$.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 );
随机推荐
- Geek荣耀大会总结
0.0 首先没有被抽中, 其次可乐真难喝,再次我没有去拍无人机合影,再再次还是很受打击的. 1.0 其实 对geek 和1024大会无感,主要原因 没有三倍加班费的节日在我眼里都不是节日. 上面只是简 ...
- laya3d 文件格式
先认识下laya3d的一些文件 导出文件说明: ls --- 场景文件: Json文件,包含场景中所有节点的数据信息,包含光照贴图信息 ...
- javascript之彻底理解valueOf, toString
参与运算的都是简单类型(一般就字符串和数字), 复杂类型是不参与运算的. ***当对象(非简单类型)用作键时,会先调用toString()方法把对象转化成字符串 var a = {}, b = ...
- PHP内置标准类
PHP内置标准类 php语言内部,有“很多现成的类”,其中有一个,被称为“内置标准类”. 这个类“内部”可以认为什么都没有,类似这样: class stdclass{ } 其作用,可以用于存储一些临 ...
- 第129天:node.js安装方法
node.js安装方法 第一步:双击node.js安装包开始安装,注意64位和32位,按照自己的进行安装 第二步:在安装过程中一直选择next,在选择安装目录时,大多数默认安装在C盘,我安装在了D盘, ...
- HDU4497——GCD and LCM
这个题目挺不错的,看到是通化邀请赛的题目,是一个很综合的数论题目. 是这样的,给你三个数的GCD和LCM,现在要你求出这三个数有多少种可能的情况. 对于是否存在这个问题,直接看 LCM%GCD是否为0 ...
- ORACLE 存储过程异常捕获并抛出
for tab_name in tables loop execute immediate 'drop table '||tab_name; --此处可能会报错 end loop; 当前情况是,循环表 ...
- Coding and Paper Letter(四十五)
资源整理. 1 Coding: 1.Python库gempy,一种基于Python的开源三维结构地质建模软件,它允许从界面和方向数据隐式(即自动)创建复杂的地质模型. 它还支持随机建模以解决参数和模型 ...
- 【BZOJ4011】【HNOI2015】落忆枫音(动态规划)
[BZOJ4011][HNOI2015]落忆枫音(动态规划) 题面 BZOJ 洛谷 Description 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜 ...
- 解题:POI 2015 Kinoman
题面 发现每种电影只在两场之间产生贡献(只有$pos$的一场的就在$[pos,n]$产生贡献).那么我们针对每个位置$i$求出这场电影下一次出现的位置$nxt[i]$,然后每次更新一下,求整个区间的最 ...