Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive ajax() requests

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?

like image 980
damon Avatar asked Aug 19 '13 09:08

damon


1 Answers

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();        
like image 66
Always Sunny Avatar answered Sep 30 '22 01:09

Always Sunny