admin管理员组文章数量:1387285
I have 5 ajax calls that look like this (not posting all five):
tabTest = {
BASE_URL : 'http://localhost/project/',
getPics : function(){
$.ajax({
type : 'GET',
url : this.BASE_URL +'js/image1.js',
dataType : "json",
async: false,
success: function(data){
var pic = data.images.image[0].image_one;
$('#pic1 img').attr("src", pic);
}
});
$.ajax({
type : 'GET',
url : this.BASE_URL +'js/image2.js',
dataType : "json",
async: false,
success: function(data){
var pic = data.images.image[0].image_two;
$('#pic2 img').attr("src", pic);
}
});
}
};
The ajax calls do work but they all fire at once; instead, I need to put a delay between each one so they do not fire all at once but rather in order only once (upon click, which happens in a different function) with 5 seconds between each call.
I've tried using 'setTimeout' but it has not worked yet. I'm really not sure how to proceed on this one.
I have 5 ajax calls that look like this (not posting all five):
tabTest = {
BASE_URL : 'http://localhost/project/',
getPics : function(){
$.ajax({
type : 'GET',
url : this.BASE_URL +'js/image1.js',
dataType : "json",
async: false,
success: function(data){
var pic = data.images.image[0].image_one;
$('#pic1 img').attr("src", pic);
}
});
$.ajax({
type : 'GET',
url : this.BASE_URL +'js/image2.js',
dataType : "json",
async: false,
success: function(data){
var pic = data.images.image[0].image_two;
$('#pic2 img').attr("src", pic);
}
});
}
};
The ajax calls do work but they all fire at once; instead, I need to put a delay between each one so they do not fire all at once but rather in order only once (upon click, which happens in a different function) with 5 seconds between each call.
I've tried using 'setTimeout' but it has not worked yet. I'm really not sure how to proceed on this one.
Share Improve this question asked Mar 21, 2013 at 11:53 max7max7 7982 gold badges16 silver badges37 bronze badges 03 Answers
Reset to default 3If you put your AJAX calls its own setTimeout()
there's no reason why it shouldn't work:
setTimeout(function(){
$.ajax({
type : 'GET',
url : this.BASE_URL +'js/image1.js',
dataType : "json",
async: false,
success: function(data){
var pic = data.images.image[0].image_one;
$('#pic1 img').attr("src", pic);
}
});
}, 5000);
And then increment the duration by 5000 for each call. So the second would look like:
setTimeout(function(){
$.ajax({
type : 'GET',
url : this.BASE_URL +'js/image2.js',
dataType : "json",
async: false,
success: function(data){
var pic = data.images.image[0].image_one;
$('#pic2 img').attr("src", pic);
}
});
}, 10000);
You could also put the functions inside the previous request's callback although a request fail would mean that all subsequent AJAX requests wouldn't get called.
Try to add next ajax call on previous success callback function.
$.ajax({
type : 'GET',
url : this.BASE_URL +'js/image1.js',
dataType : "json",
async: false,
success: function(data){
var pic = data.images.image[0].image_one;
$('#pic1 img').attr("src", pic);
$.ajax({
type : 'GET',
url : this.BASE_URL +'js/image2.js',
dataType : "json",
async: false,
success: function(data){
var pic = data.images.image[0].image_two;
$('#pic2 img').attr("src", pic);
}
});
}
});
OR
$.ajax({
type : 'GET',
url : this.BASE_URL +'js/image1.js',
dataType : "json",
async: false,
success: function(data){
var pic = data.images.image[0].image_one;
$('#pic1 img').attr("src", pic);
call2();
}
});
function call2(){
$.ajax({
type : 'GET',
url : this.BASE_URL +'js/image2.js',
dataType : "json",
async: false,
success: function(data){
var pic = data.images.image[0].image_two;
$('#pic2 img').attr("src", pic);
}
});
}
So now when the first ajax will pleted then next call will fire.
You can use setInterval()
for this. When using setInterval()
it can cal the wat u mention the time for function...u check it out the seconds ...
setInterval("functionname()",50000);
本文标签: javascriptDelay between multiple ajax callsStack Overflow
版权声明:本文标题:javascript - Delay between multiple ajax calls - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744542972a2611725.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论