0
    function test(){
    var distance=null;
           first();      
    alert(distance);//it shows null always because it take 2 second to complete.

    }
     function first(){
 $.post("v.....fi.aspx", { "func": "xxxxx", xxxxx: xxxx},                                                                        function (data) {     
 distance=dis;  // update the value of distance but it takes 2 second to complete.
});  
    }

But the alert does not shows the distance value.please help me

Venkatvasan
  • 491
  • 3
  • 13

1 Answers1

1

AJAX is an Asynchronous request. So anything that uses the outcome should be inside the success function. So, what you need to do is:

function test(){
  var distance=null;
  first();
}
function first(){
  $.post("v.....fi.aspx", { "func": "xxxxx", xxxxx: xxxx}, function (data) {
    distance=dis;
    alert(distance);
  });  
}

Now this should work.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252