0

i use for loop for check validation if data get success then data will store in one variable i want to console that variable after execute for loop but currently in my code that variable call before for loop i use this code

function UpdateOnlineDriver(data) {
    console.log(data);
    onlinedriverdata = [];
    var sessionvar =  $('#getsession').val();    
    if(sessionvar == '2'){
        for (var i = 0; i < data.length; i++){        
            var ddata = data[i];
            $.ajax({
                type:"post",
                url: "data/chk_baseadmin.php",
                data: {did:data[i][0]}
            }).success(function(data){
                //console.log(data);
                if(data == 'yes'){
                    console.log(ddata[0]);
                    onlinedriverdata.push([ddata[0],ddata[1],ddata[2],ddata[3],ddata[4]]);
                }  
            });        
        }
    }
    else{
        onlinedriverdata = data;
    }   
    console.log(onlinedriverdata);
}

when execute this function console vareable first then execute for loop i want to console that variable after complete execute for loop so give me proper solution for this issue

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Ajax calls are asynchronous, meaning the code keeps going while the ajax call is happening. That means that your console.log on the last line happens before the ajax call has finished. – Caleb O'Leary May 03 '16 at 17:32
  • but i want to store all data in onlinedriverdata using push then console all data so if you have any idea so tell me – Trushar narodia May 03 '16 at 17:35
  • @Trush: Search Stack Overflow for how to "wait" for multiple Ajax calls. – Felix Kling May 03 '16 at 17:36

0 Answers0