Javascript 函数中的变量

Javascript 函数中的变量,javascript,variables,global-variables,Javascript,Variables,Global Variables,问题在于变量和函数返回。这段代码帮助连接到mysql数据库并选择两个城市之间的距离。变量ret_pomoc是距离 ret\u pomoc中的值我需要在另一个函数中使用,例如在另一个函数()中 以下是代码: var ret_pomoc; var vzdialenost; function ajaxFunction(mesto_1,mesto_2){ var ajaxRequest; // The variable that makes Ajax possible!

问题在于变量和函数返回。这段代码帮助连接到mysql数据库并选择两个城市之间的距离。变量
ret_pomoc
是距离

ret\u pomoc
中的值我需要在另一个函数中使用,例如在
另一个函数()

以下是代码:

var ret_pomoc;
var vzdialenost;

function ajaxFunction(mesto_1,mesto_2){
            var ajaxRequest;  // The variable that makes Ajax possible!
            try{
               // Opera 8.0+, Firefox, Safari
               ajaxRequest = new XMLHttpRequest();
             }catch (e){
               // Internet Explorer Browsers
               try{
                  ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
               }catch (e) {
                  try{
                     ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                  }catch (e){
                     // Something went wrong
                     alert("Your browser broke!");
                     return false;
                  }
               }
             } 
     // Create a function that will receive data 
     // sent from the server and will update
     // div section in the same page.

     ajaxRequest.onreadystatechange = function(){
       if(ajaxRequest.readyState == 4){
          var ajaxDisplay = document.getElementById('mapa2');
          ajaxDisplay.value = ajaxRequest.responseText;



        ret_pomoc=ajaxDisplay.value; //HERE IS KEY VARIABLE
        console.log(ret_pomoc); // 1. Here it is OK show 91

       }

     } 

    console.log(ret_pomoc); // 2. Here it does not work show undefined and return does not work 

     // Now get the value from user and pass it to
     // server script.
            var z_mesta = mesto_1;
            var do_mesta = mesto_2;
            var queryString = "?z_mesta=" + z_mesta ;
            queryString +=  "&do_mesta=" + do_mesta;
            ajaxRequest.open("GET", "pristup.php" + 
                                  queryString, true);
            ajaxRequest.send(null); 


    } 
我需要在另一个函数中使用
ret_pomoc
中的值

function another_function(){

    vzdialenost=ajaxFunction('Bratislava','Nitra') + ajaxFunction('Poprad','Nitra') ;

    }

ajax请求是异步工作的,因此实现这一点的方法是传递一个
回调
函数,该函数将在响应返回时调用

可能重复的
function some_function2(url, callback) {
    var httpRequest; // create our XMLHttpRequest object
    if (window.XMLHttpRequest) {
        httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // Internet Explorer is stupid
        httpRequest = new
            ActiveXObject("Microsoft.XMLHTTP");
    }

    httpRequest.onreadystatechange = function() {
        // inline function to check the status
        // of our request
        // this is called on every state change
        if (httpRequest.readyState === 4 &&
                httpRequest.status === 200) {
            callback.call(httpRequest.responseXML);
            // call the callback function
        }
    };
    httpRequest.open('GET', url);
    httpRequest.send();
}
// call the function
some_function2("text.xml", function() {
    console.log(this);
});
console.log("this will run before the above callback");