首页 > 代码库 > $.when()方法翻译2
$.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 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 = http://www.mamicode.com/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 functionmyFunc
when both ajax requests are successful, ormyFailure
if either one has an error.$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) )
.then( myFunc, myFailure );
当两个ajax请求成功后,执行函数myFunc,或者在二者之一出错后执行myFailure.
$.when()方法翻译2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。