Reputation: 53
I have this post code inside a loop (while i<10) and I am trying to delay each post for 5 sec but instead what is doing is delaying 5 sec and sends all 10 posts at once
Is there a way to do interval or delay each post in jQuery or Ajax inside a loop?
$(document).ready(function() {
setTimeout(function(){
$.post("Trigger.aspx", { phone: phoneval,
sms: smsval }, function(data) {
$('.result').html(data);
});
},5000);
return false;
});
Upvotes: 1
Views: 764
Reputation: 318518
Simply use an increasing delay:
$(document).ready(function() {
for(var i = 0; i < 10; i++) {
setTimeout(function(){
$.post("Trigger.aspx", {
phone: phoneval,
sms: smsval
}, function(data) {
$('.result').html(data);
});
}, 5000 * i);
}
});
If the first POST should be delayed by 5 seconds, use 5000 * (i + 1)
or change your loop to for(var i = 1; i <= 10; i++)
.
Upvotes: 1
Reputation: 14874
as far as i remember jquery's ajax call has a beforeSend function that maybe addressed your problem
Upvotes: 1