完成多个获取请求后调用jquery函数

问题描述:

好的,因此在我的网站上,我的加载主页通过使用.get方法创建了4个轮播.我想知道在每个轮播成功加载后如何调用js函数.有时有3到4个,有时没有任何旋转木马.无论如何,我是否正在使用get请求跟踪他们?

alright so on my site my home page on load create 4 carousels by using the .get method. I am wondering how one might call a js function after every carousel has been successfully loaded. Sometimes there are 3 to 4 sometimes there aren't any carousels. Is their anyway of tracking if i am requesting anything using get?

感谢一堆

Jquery最近引入了推迟对象,并让每个ajax调用都返回一个 jqxhr 对象承诺.所有这些意味着您可以使用以下语法摆脱疯狂情况的回调金字塔:

Jquery recently introduced the Deferred object and made every ajax call return an jqxhr object that works as a promise. All this means you can get out of the callback pyramid of madness situations with a syntax like this:

$.when(
    $.get('http://example.com'),
    $.get('http://example.com'),
    $.get('http://example.com')
).done(function(resp1, resp2, resp3){
    console.log(resp1, resp2, resp3);
    alert('all done \o/');
});

注意:仅当所有先前的诺言都成功返回时,才会调用done函数.对于不运行回调的情况,请使用始终.为了仅捕获错误,有失败.

Note: the done function(s) will be called only when all previous promise returns successfully. For a callback that runs no mater what use always. For catching only errors there's fail.