最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delay between multiple ajax calls - Stack Overflow

programmeradmin2浏览0评论

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 0
Add a ment  | 

3 Answers 3

Reset to default 3

If 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);    
发布评论

评论列表(0)

  1. 暂无评论