$.each(emails, function(index, value)
{
$.post('ajax/send_email.php', { email: value, ... }, function(data)
{
$('#emails_sent').text('Sent ' + (index + 1) + '/' + num_emails);
});
});
在
$(function()
{
$('#cancel').on('click', function(e)
{
hidePopupWindow();
});
});
虽然$.each
没有运行任何按钮,例如取消工作 - 但无法选择或按下它们。
答案 0 :(得分:1)
您可以修改您的程序以使用维护当前存在的xhr请求数组,然后在取消时您可以中止XHR。请参阅以下代码:
var globalAbort = false,
xhrPool = [];
$.each(emails, function(index, value) {
if (!globalAbort) {
$.ajax("ajax/send_email.php", {
beforeSend: function(xhr){
xhrPool.push(xhr);
},
email: value,
success: function(data, status, xhr){
var index = xhrPool.indexOf(xhr);
if (index > -1) {
xhrPool.splice(index, 1);
}
$('#emails_sent').text('Sent ' + (index + 1) + '/' + num_emails);
}
});
}
});
$('#cancel').on('click', function(e) {
var xhrLen = xhrPool.length;
for (var i = 0; i < xhrLen; i++) {
xhrPool[i].abort();
}
xhrPool.length = 0;
hidePopupWindow();
});