$.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 );
随机推荐
- 高性能的HTTP代理 LittleProxy
引用: https://github.com/adamfisk/LittleProxy 拦截和操纵HTTPS流量,LittleProxy使用中间人(MITM)管理器. LittleProxy的默认实现 ...
- 【C】多线程编程笔记
1. pthread_create(pthread类型指针变量 ,NULL ,函数 ,函数参数[多个参数用结构体传]) 2. pthread_join(pthread类型指针变量, 返回一般为null ...
- 【Linux笔记】GRUB配置与应用,启动故障分析解决。
一.GRUB启动位置 GRUB是现今大多数Linux系统采用的自举程序,这里先来看一下Linux的程序顺序: 执行顺序 动作 固件Firmware(CMOS/BIOS) → POST(Pwer ...
- bzoj3545-bzoj3551-Peaks
题意 给出一个图,边有边权,点有点权,每次询问一个点 \(x\) 只走边权小于等于 \(d\) 的边能到达的点中点权第 \(k\) 大. 强制在线,\(n\le 10^5,m,q\le 5\times ...
- BZOJ4892 Tjoi2017dna(后缀数组)
对每个子串暴力匹配至失配三次即可.可以用SA查lcp.然而在bzoj上被卡常了.当然也可以二分+哈希或者SAM甚至FFT. #include<iostream> #include<c ...
- C++解析(15):二阶构造模式
0.目录 1.构造函数与半成品对象 2.二阶构造 3.小结 1.构造函数与半成品对象 关于构造函数: 类的构造函数用于对象的初始化 构造函数与类同名并且没有返回值 构造函数在对象定义时自动被调用 问题 ...
- Qt浅谈内存泄露(总结)
Qt浅谈内存泄露(总结) 来源 http://blog.csdn.net/taiyang1987912/article/details/29271549 一.简介 Qt内存管理机制:Qt 在内部能够维 ...
- 【HDU5919】SequenceII(主席树)
[HDU5919]SequenceII(主席树) 题面 Vjudge 翻译(by ppl) 给一个长度为N的数列A,有m个询问,每次问 数列[l,r]区间中所有数的第一次出现的位置的中位 数是多少 题 ...
- MySQL 5.5 主从复制
MySQL 5.5 主从复制的原理.过程 分为同步复制和异步复制,实际复制架构中大部分为异步复制.复制的基本过程如下: 1).Slave上面的IO进程连接上Master,并请求从指定日志文件的指 ...
- linux内核分析 第四周 扒开系统调用的三层皮(上)
一.用户态.内核态和中断处理过程 系统调用是用户通过库函数方式:库函数帮我们把系统调用封装起来. 内核态:高级别执行,可以使用特权指令,访问任意的物理地址. 用户态:低级别执行,代码范围受到限制. C ...