I want to send email to > 2000 recipients. I make a ajax call that sends 50 email and i want to repeat it until all emails will send. PHP function returns an array with 3 index, Success (count success mail), failed (count failed mail) and Finish (determine no more email exists). So, I get email count to counts and I use a counter (i) to determine how many email is sent. I want to break loop when i >= counts. Here is my code:
我想给>2000的收件人发邮件。我打了一个ajax电话,发了50封电子邮件,我想重复一次,直到所有的电子邮件都发送出去。PHP函数返回一个数组,其中包含3个索引、成功(计数成功邮件)、失败(计数失败邮件)和Finish(确定不再存在电子邮件)。因此,我得到电子邮件计数计数和我使用计数器(I)确定发送了多少电子邮件。当>= counts时,我想要打破循环。这是我的代码:
function sendEmailStepByStep(imageAddress, counts) {
$(".feature div").append("<br />Please wait...<div class='feature_process'> </div>" + "<div class='feature_message'>Success: <span id='success_email'>0</span>, Failed: <span id='failed_email'>0</span></div>");
var i = 0;
timer = setTimeout(function () {
$.ajax({
type: "GET",
url: "",
dataType: 'json',
data: {
group: 'packages',
do :'emailPackageUsingAjax',
image: imageAddress,
num: i
}
}).done(function (response) {
console.log('success');
console.log(response);
$("#success_email").html(response.Success);
$("#failed_email").html(response.Failed);
i = i + response.Success + response.Failed;
if (i > counts || response.Finish == 'true') {
clearTimeout(timer);
}
}).fail(function (response) {
$(".feature_message").append(response.responseText);
console.log("error");
console.log(response);
clearTimeout(timer);
})
}, 1000);
}
Why this is called just once?
为什么只调用一次?
1 个解决方案
#1
3
Instead of using setTimeout or setInterval I would recommend something like this:
我不建议使用setTimeout或setInterval,而是推荐如下方法:
function sendEmailStepByStep(imageAddress, counts) {
$(".feature div").append("<br />Please wait...<div class='feature_process'> </div>" + "<div class='feature_message'>Success: <span id='success_email'>0</span>, Failed: <span id='failed_email'>0</span></div>");
var i = 0;
var mailer = function () { // Create internal function instead of timer
$.ajax({
type: "GET",
url: "",
dataType: 'json',
data: {
group: 'packages',
do :'emailPackageUsingAjax',
image: imageAddress,
num: i
}
}).done(function (response) {
console.log('success');
console.log(response);
$("#success_email").html(response.Success);
$("#failed_email").html(response.Failed);
i = i + response.Success + response.Failed;
if (i < counts && response.Finish != 'true') {
mailer(); // if count is less and not finished, invoke function again
}
}).fail(function (response) {
$(".feature_message").append(response.responseText);
console.log("error");
console.log(response);
})
};
mailer(); // First invocation of function
}
Using above technique will ensure that next batch is sent only when the previous one is completed, even when it takes more than 1 sec, as you set in timeout.
使用上述技术将确保下一批只在前一批完成时发送,即使在设置超时时需要超过1秒。
#1
3
Instead of using setTimeout or setInterval I would recommend something like this:
我不建议使用setTimeout或setInterval,而是推荐如下方法:
function sendEmailStepByStep(imageAddress, counts) {
$(".feature div").append("<br />Please wait...<div class='feature_process'> </div>" + "<div class='feature_message'>Success: <span id='success_email'>0</span>, Failed: <span id='failed_email'>0</span></div>");
var i = 0;
var mailer = function () { // Create internal function instead of timer
$.ajax({
type: "GET",
url: "",
dataType: 'json',
data: {
group: 'packages',
do :'emailPackageUsingAjax',
image: imageAddress,
num: i
}
}).done(function (response) {
console.log('success');
console.log(response);
$("#success_email").html(response.Success);
$("#failed_email").html(response.Failed);
i = i + response.Success + response.Failed;
if (i < counts && response.Finish != 'true') {
mailer(); // if count is less and not finished, invoke function again
}
}).fail(function (response) {
$(".feature_message").append(response.responseText);
console.log("error");
console.log(response);
})
};
mailer(); // First invocation of function
}
Using above technique will ensure that next batch is sent only when the previous one is completed, even when it takes more than 1 sec, as you set in timeout.
使用上述技术将确保下一批只在前一批完成时发送,即使在设置超时时需要超过1秒。