I'll try to give you an explanation so you understand what you did, by your answers I believe you wrote but did not understand.
The setTimeout will run the code contained in your function, which is a callback, only after the time set in the second parameter of that function in the case below is 3000 milliseconds which corresponds to 3 seconds. / p>
setTimeout(function(){ alert("Hello"); }, 3000);
So if I set the time as below:
$("#execute").click(function(){
var email = $("#email").val();
var index = 0;
splitemail = email.split("\n");
splitemail.forEach(function(value){
setTimeout(function(){
$.ajax({
url:'ajax.php',
method:'GET',
data: { email: value }
}).done(function(retorno){
$("#ren").html(retorno);
})
}, 1000 * index);
index = index + 1;
});
is saying that it is to send the request every 1000 milliseconds X index (1000 X index). But who is index, index is a variable contained in your local scope of the onclick event that is triggered when there is a click on the button on your form defined by the id #execute . The index variable is incremented by +1 to each interaction of your foreach, which in the case is a loop defined by the amount of items contained in your splitemail array . Therefore, as the ajax request will be invoked depending on the amount of line breaks contained in your textarea each foreach interaction will multiply 1000 milliseconds per index, in the first loop interaction its time will be 1000x0 once index is started with zero, in the second loop interaction setTimeout will have the time parameter of 1000 * 1 = 1 second (because index is 1), and the third time will have the parameter 1000 * 2 = 2 seconds because index will be two and so on. So porting will not be 1 second as I said in your question and reply to my comment. With each new interaction the shipping time gradually goes up because it is multiplying index times 1 second.
If setTimeout will wait for the time set in the parameter would just suffice.
setTimeout(function(){
$.ajax({
url:'ajax.php',
method:'GET',
data: { email: value }
}).done(function(retorno){
$("#ren").html(retorno);
})
}, 1000);
Your question refers to how you could count the number of items already shipped. In your routine you set and variable index, with each increment of the loop the variable is incremented by 1. So at the end of the foreach interaction of your array the total value of index will be the amount of items in your array splitemail . And if you want to know the total amount is as I said in comment after the split of the value contained in your textarea just use the lenght in the array to get the total amount of messages contained in the listing.
/* Devolve a quantidade total de mensagens contidas no campo textarea
após transforma-las em uma array (uma lista) explodindo a string com o \n
(quebra de linha) utilizando a função split */
var qtd_linhas = splitemail.length;
I've set up this example by printing the time value in setTimeout to understand it better.
Routine Shipping Test
Insert text-wrapping emails in textarea, and then click RUN :
This example would be correct, with the amount of emails sent displaying in your div.
click to test
$("#execute").click(function(){
var email = $("#email").val();
var index = 1;
splitemail = email.split("\n");
splitemail.forEach(function(value){
alert("O tempo de espera é : "+(1000 * index));
setTimeout(function(){
alert(value);
$('#cnt_enviados').text("E-Mails Enviados: "+index);
index = index + 1;
}, 1000);
});
});