首先可以总结一下when的作用:
使用jQuery中的when实现多个AJAX请求对应单个回调
jQuery.when(deferreds)
参数deferreds,一个或多个延时对象或JS对象,我们初略的认为它就是一个或多个异步请求。
例如:
- $.when($.ajax("page1.php"), $.ajax("page2.php"))
when()函数常常和done()函数、fail()函数、then()函数联合使用:
done(Function func) - 当deferreds中的处理都完成的时候执行Function回调函数
fail(Function func) - 当deferreds中有一个处理失败的时候执行Function回调函数
then(Function func1,Function func2)- 结合了done和fail函数,当都成功执行func1,当有一个失败执行func2
Example:
- var whenResult = $.when($.ajax("page1.php"), $.ajax("page2.php"));
- whenResult.done(function(a1,a2){
- //函数内容略
- //a1和a2俩参数是when函数中两个ajax请求的相关jqXHR对象
- });
- whenResult.fail(function(){
- //函数内容略
- })
- whenResult.then(successFunc,failureFunc);
相关推荐
对于$.ajax请求来说,如果层级比较多,程序看起来会比较乱,而为了解决这种问题,才有了$when…done…fail…then的封装,它将$.ajax这嵌套结构转成了顺序平行的结果,向下面的$.ajax写法,看起来很乱 $.ajax({ url...
We already have a basic working knowledge of the components when we add a new component because of the consistency in how we implement the different components that make up the library. Therefore, we ...
Use selectors in a variety of ways to get anything you want from a page when you need it Master events to bring your web pages to life Add flair to your actions with a variety of different animation ...
Chapter 12 When jQuery is not enough... plugins to the rescue! Chapter 13 Avoiding the callback hell with Deferred Chapter 14 Unit testing with QUnit Chapter 15 How jQuery fits into large projects ...
jQuery UI Cookbook(2013.7) Adam ...Explore coding conventions that work best when writing jQuery UI code Learn about unique perspectives on approaches to widget development – maintainability matters
Highlight GridView Rows when Checkbox is checked using jQuery Highlight Negative value columns in ASP.NET GridView using jQuery Searching and Filtering Related How to loop through all grid ...
Implements automatic *Cross Origin Resource Sharing* support using the `XDomainRequest` object for IE8 and IE9 when using the [$.ajax](http://api.jquery.com/jQuery.ajax/) function in jQuery 1.5+. ...
When combined with color and class animations and custom easings that jQuery UI provides, there are even more ways to create animations. A good rule of thumb for using animations is to use slides ...
def.when(deferreds)1.5+ Traversing Filtering $.eq( index ) $.first( ) $.last( ) $.has( selector ), .has( element ) $.filter( selector ), .filter( fn(index) ) bool.is( selector ) $.map( fn(index, ...
Whenever a matched element is clicked, the first specified function is fired, when clicked again, the second is fired. All subsequent clicks continue to rotate through the two functions. Use unbind(...
Many web developers don’t seem to know exactly when they need to use jQuery, and when they don’t – it has become a standard step for many when setting up a new library or web application....
Many web developers don't seem to know exactly when they need to use jQuery, and when they don't - it has become a standard step for many when setting up a new library or web application. But relying...
Thanks to jQuery, no one remembers the bad old days when programmers manually managed browser inconsistencies, CSS selectors support, and DOM navigation, and when every animation was a frustrating ...
jQuery has become a part of my everyday routine in Web design and development, so when Wiley approached me about writing a book on jQuery, I was thrilled. jQuery has opened up a world of possibility...
Auto-Save your content when editing in HTML input and textarea DOM element. http://geniuscarrier.com/autosave-a-jquery-plugin/
(Bug) Fixed an issue where host and protocol were not compared case-insensitively when determining whether anAJAXrequest was local or remote (#6908) (Bug) Fixed an issue where the “clone” variable ...
jquery3.2.1的min版,开发版,地图都含有 jQuery3.X特性: 1. 移除旧的IE工作区 2.jQuery 3.0运行在Strict Mode下 ...8. jQuery.when()对于多种参数的不同解读 9. 新的显示/隐藏逻辑 10. 对于防止XSS攻击的额外保护
jQuery blockUI plugin Version 2.39 (23-MAY-2011) Demos: $(document).ready(function() { $('#demo1').click(function() { $.blockUI({ message: $('#loginForm') }); setTimeout($.unblockUI, 2000); ...