当这两个$.get
调用完成后,如何执行某些代码?
if (invoice.is(":checked")) {
var data = {
save: 1,
order_id: order_id
}
jQuery.get('http://domain.co.uk/customer_orders/edit/view_pdf', data, function(response) {});
}
var receipt = $(this).parents(".admin_email_preview_container").find("input[name='receipt']");
if (receipt.is(":checked")) {
var data = {
save: 1,
order_id: order_id,
invoice: 1
}
jQuery.get('http://domain.co.uk/customer_orders/edit/view_pdf', data, function(response) {});
}
我想在上述两个调用完成后再运行另一个$.get
调用。我该如何解决这个问题?
答案 0 :(得分:1)
您可以将<div ng-repeat="value in present | filter:searchText">
返回的承诺存储在数组中,并将其应用于$.get
。试试这个:
$.when()
请注意,我通过一致使用var requests = [];
if ($invoice.is(":checked")) {
requests.push($.get('http://domain.co.uk/customer_orders/edit/view_pdf', {
save: 1,
order_id: order_id
}));
}
var $receipt = $(this).parents(".admin_email_preview_container").find("input[name='receipt']");
if ($receipt.is(":checked")) {
requests.push($.get('http://domain.co.uk/customer_orders/edit/view_pdf', {
save: 1,
order_id: order_id,
invoice: 1
}));
}
$.when.apply(requests).done(function() {
// both $.get calls have completed, run other code here...
$.get('/other-endpoint');
});
代替$
来略微整理您的代码(如果您需要防止jQuery
变量的污染,请使用闭包),我感动对象直接进入$
调用,以便更容易阅读,并删除冗余的空回调函数。