码迷,mamicode.com
首页 > 其他好文 > 详细

$.when()方法翻译2

时间:2017-05-25 23:34:53      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:define   orm   .ajax   注意   exec   after   ring   jquery   文章   

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 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 );
在一个事件中,deferred无完成值,对应的done参数将为undefined。如果一个deferred完成后仅一个值,读经的参数将获取到那个值。这样,deferred完成值为复合值,那对应的参数将是一个数组。例子如下:见上。
 
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.
在复合deferreds中,其中一个失败了,jQuery.when()立即启动fail回调作为其主deferred。注意,在那个点有些deferreds或许仍旧处于未完成状态。传递给fail回调的参数会为失败的deferred匹配到fail回调的标记。这种情况下如果你需要执行额外的进程,诸如取消未完成的ajax请求,可以使用闭包保持对这个jqXHR对象的引用,并在fail回调中对其取消或检验。
 

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!" );
}
});
在两个ajax请求成功后执行一个函数。(查看jQuery.ajax()文档获取完整的关于成功和错误状态的ajax请求的文档。)
 
Execute the function myFunc when both ajax requests are successful, or myFailure if either one has an error.
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) )
.then( myFunc, myFailure );
当两个ajax请求成功后,执行函数myFunc,或者在二者之一出错后执行myFailure.

$.when()方法翻译2

标签:define   orm   .ajax   注意   exec   after   ring   jquery   文章   

原文地址:http://www.cnblogs.com/jiangtian/p/6906216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!