如何呼叫警报所有Ajax请求完成后? [英] How to call alert after all ajax requests are done?

查看:111
本文介绍了如何呼叫警报所有Ajax请求完成后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做在codeA与jQuery几个请求和 GET 。它看起来像:

I'm making in code a few requests with JQuery and get. It looks like:

$.get('address1', function() { ... });
$.get('address2', function() { ... });
$.get('address3', function() { ... });

// This code should be runned when all 3 requests are finished
alert('Finished');

那么,有什么方法来检测是否还有处理请求并运行,只有当所有3请求完成后打上code。

So, are there any ways to detect whether there is still processing request and run marked code only when all 3 requests are finished.

感谢。

推荐答案

您可以使用延期对象 [文档] jQuery的1.5介绍:

You can make use of deferred objects [docs] introduced in jQuery 1.5:

$.when(
    $.get('address1', function() { ... }),
    $.get('address2', function() { ... }),
    $.get('address3', function() { ... })
).then(function() {
    alert('Finished');
});

参考: jQuery.when

jQuery的学习中心有一个很好的介绍推迟对象/ 的承诺

The jQuery learning center has a nice introduction to deferred objects / promises.

这篇关于如何呼叫警报所有Ajax请求完成后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆