Trying to delay a jQuery post
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 }, func开发者_JAVA百科tion(data) {
$('.result').html(data);
});
},5000);
return false;
});
as far as i remember jquery's ajax call has a beforeSend function that maybe addressed your problem
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++)
.
精彩评论