首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >函数中的变量

函数中的变量
EN

Stack Overflow用户
提问于 2013-01-25 19:57:39
回答 1查看 53关注 0票数 0

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

例如,在another_fuction()中,我需要在另一个函数中使用ret_pomoc中的值。

下面是代码:

代码语言:javascript
运行
复制
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的值

代码语言:javascript
运行
复制
function another_function(){

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

    }
EN

回答 1

Stack Overflow用户

发布于 2013-01-25 20:02:44

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

Understanding callback functions in Javascript

代码语言:javascript
运行
复制
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");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14521360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档