I hope everyone will be in Good health.
I am working on jQuery for fetching multiple records against some id. The record here means Email Address.
Required: I want to call the 5 fetch_email functions after completing the previous fetch_email function.
Issue in this Code: It is calling the 5 fetch_email functions at the same time. Because of calling at the same time, I am getting the emails but not in a Sequence.
Desired Result: I want to get the emails in this way. (In a Sequence)
- email1@email.com
- email2@email.com
- email3@email.com
- email4@email.com
- email5@email.com
Kindly Help. Thanks
<script>
var serial_no = 0;
var msg = '';
function fetch_email (id, show)
{
var form = new FormData();
form.append("id", id);
$.ajax(
{
url: "https://www.example.com/action.php",
method: "POST",
data: form,
success: function(response)
{
serial_no = serial_no+1;
msg = msg + serial_no + ". " + response + "<br />";
if(show == 1)
{
document.write(msg);
}
}
});
}
$(window).bind("load", function()
{
for (var i = 1; i < 6; i++)
{
fetch_email (i, 0);
}
fetch_data (i, 1);
});
</script>