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

javascript - Recursive ajax() requests - Stack Overflow

programmeradmin1浏览0评论

I use jQuery's ajax()to get information. I call the method when the request is successful. Here is the code:

function recursively_ajax(){
    console.warn("begin");
    $.ajax({
        type:"GET",
        url: "./JvmInfoClass",
        success: function(data){
            console.warn("get jvm info success");
            recursively_ajax();
        }
    });
}

recursively_ajax();

I make the thread sleep 3 seconds in the back-end. But the console print the message continuously not after 3 seconds. Why is this?

I use jQuery's ajax()to get information. I call the method when the request is successful. Here is the code:

function recursively_ajax(){
    console.warn("begin");
    $.ajax({
        type:"GET",
        url: "./JvmInfoClass",
        success: function(data){
            console.warn("get jvm info success");
            recursively_ajax();
        }
    });
}

recursively_ajax();

I make the thread sleep 3 seconds in the back-end. But the console print the message continuously not after 3 seconds. Why is this?

Share Improve this question edited Jun 26, 2015 at 13:52 Sumurai8 20.7k11 gold badges68 silver badges102 bronze badges asked Aug 19, 2013 at 9:19 damondamon 2691 gold badge3 silver badges12 bronze badges 3
  • Try if(data){ console.log(); } and also share server-side code. – Dipesh Parmar Commented Aug 19, 2013 at 9:21
  • 2 Is your server caching the response and returning it immediately after the first one? i.e. is there a 3 second delay the very first time after clearing your browser cache. – iCollect.it Ltd Commented Aug 19, 2013 at 9:24
  • Spot on. The problem has been resolved. Many thanks. – damon Commented Aug 19, 2013 at 10:04
Add a comment  | 

3 Answers 3

Reset to default 13

You can try this with ajax call async:false

var counter=0;
 function recursively_ajax()
{
var pass_data=5;
var chartMenu=['VTR','NC','RC','TOCU','TOCO','TR','COA','MP'];
$.ajax({
        type:"POST",
        async:false, // set async false to wait for previous response
        url: "url to server",
        dataType:"json",
        data:{dataMenu:pass_data},
        success: function(data)
        {
            counter++;
            if(counter < chartMenu.length){
                recursively_ajax();
            }
        }
    });
 }      
 recursively_ajax();        

in that case the bug is in the server side code because the server should sent back the response only after 3 seconds.

But I would recommend to use setTimeout() in the client side to restrict the request frequency

Try

function recursively_ajax(){
    console.warn("begin");
    $.ajax({
        type:"GET",
        url: "./JvmInfoClass",
        success: function(data){
            console.warn("get jvm info success");
            setTimeout(recursively_ajax, 3000)
        }
    });
}
recursively_ajax();

It's browser's cacheing problem,I append the date to the url or set the ajax cache:false,the problem is solved.Thank everyone.

发布评论

评论列表(0)

  1. 暂无评论