2011-02-25 136 views
0

我不明白为什么我的全局变量workRegionCode设置不正确。为什么没有设置全局变量值?

在功能getWorkRegion(),让Ajax回调之后,它尝试设置workRegionCode全局变量。 (在功能setFirstIndexWorkRegionCode()内)。

期望值像401或123等

但在setFirstIndexWorkRegionCode()输出警报然后当getMachines()被调用时,全局变量是workRegionCode :(undefiend

这个js从开始的window.onload()

(请忽略这些日本JSON键值和日本少数变量他们是无害的。)

编号:

var workRegionDropdown = document.getElementById("workRegionDropdown");; 
    var machineDropdown = document.getElementById("machineDropdown"); 

    //this is the global variable with problem..... 
    var workRegionCode; 

    //INIT 
    window.onload = function() { 

     getWorkRegions(); 

     // alert("before: " + window.workRegionCode); 

     getMachines(); 

     // alert("after: " + window.workRegionCode); 
    } 

    function addWorkRegionToDropdown(jsonObject, dropdown) { 
     for(var i=0, j=jsonObject.length; i<j; i++) { 
      var option = document.createElement("option"); 
      option.text = jsonObject[i].作業区コード + ":" + jsonObject[i].作業区名漢字; 
      option.value = jsonObject[i].作業区コード; 
      dropdown.options.add(option); 
     } 
    } 

    function addMachineToDropdown(jsonObject, dropdown) { 
     for(var i=0, j=jsonObject.length; i<j; i++) { 
      var option = document.createElement("option"); 
      option.text = jsonObject[i].機械名; 
      option.value = jsonObject[i].機械名; 
      dropdown.options.add(option); 
     } 
    } 


    function getMachines() { 

     //!!!!!!!!!!! workRegionCode is undefined.. why?!?!?! 

     alert("inside of getMachines() ==> " + window.workRegionCode); 

     var ajaxRequest = new XMLHttpRequest(); 
     ajaxTimeout = setTimeout(function() {timeoutAjax(ajaxRequest, "showMessage")}, "5000"); 

     ajaxRequest.onreadystatechange = function() { 
      if(ajaxRequest.readyState == 4 ) { 
       clearTimeout(ajaxTimeout); 
       switch (ajaxRequest.status) { 
        case 200: 
           var jsonOut = JSON.parse(ajaxRequest.responseText); 
           addMachineToDropdown(jsonOut.機械, machineDropdown); 
           break; 
        default: 

           document.getElementById("showMessage").innerHTML = ajaxRequest.responseText; 
       } 
      } 
     } 


     var aMonthAgo = new Date(); 
     with(aMonthAgo) { 
      setMonth(getMonth() - 1) 
     } 
     aMonthAgo = aMonthAgo.getYYYYMMDD(); 
     var 終了日 = "29991231"; 

     var url = "../resources/machine/list/" + window.workRegionCode + "/" + aMonthAgo + "/" + 終了日; 
     ajaxRequest.open("GET", url, true); 
     ajaxRequest.send(null) 
    } 




    function getWorkRegions() { 
     var ajaxRequest = new XMLHttpRequest(); 
     ajaxTimeout = setTimeout(function() {timeoutAjax(ajaxRequest, "showMessage")}, "5000"); 

     ajaxRequest.onreadystatechange = function() { 
      if(ajaxRequest.readyState == 4 ) { 
       clearTimeout(ajaxTimeout); 
       switch (ajaxRequest.status) { 
        case 200: 
           var jsonOut = JSON.parse(ajaxRequest.responseText); 

           //set global variable workRegionCode 
           setFirstIndexWorkRegionCode(jsonOut); 

           addWorkRegionToDropdown(jsonOut.作業区, workRegionDropdown); 
        default: 

           document.getElementById("showMessage").innerHTML = ajaxRequest.responseText; 
       } 
      } 
     } 

     var url = "../resources/workshop/list"; 
     ajaxRequest.open("GET", url, true); 
     ajaxRequest.send(null) 
    }//end getWorkRegions() 

    function setFirstIndexWorkRegionCode(jsonString) { 

     //here I set the value to work region code! 
     window.workRegionCode = jsonString.作業区[0].作業区コード; 
     alert("作業区コード: " + window.workRegionCode); 

    } 
+0

是的我没有“窗口”开始,但我读了一篇文章,说全局变量隐式地是窗口对象的属性。此外日文变量是好的,另一个带有jap chars的js文件工作得很好。 – 2011-02-25 07:26:48

回答

2

AJAX中的“A”代表异步。这意味着AJAX调用会被触发并且代码的其余部分会继续执行。它不会等待回应。这将是一个同步调用。如果getMachines()取决于响应getWorkRegions(),你应该做这样的事情:

window.onload = function() { 

    getWorkRegions(); 

} 


getWorkRegions() { 

... 
    if (ajaxRequest.status == 200) { 

     ... 
     var jsonOut = JSON.parse(ajaxRequest.responseText); 
     setFirstIndexWorkRegionCode(); 
     addWorkRegionToDropdown(); 

     getMachines(); 
     ... 
    } 

... 

} 
+0

现在我明白ajax机制了..谢谢! :) – 2011-02-25 07:25:21

1

workRegionCode的值赋给在异步AJAX请求的回调函数,但是你想读创建AJAX请求的线程中的值。 getMachines()始终在您的ajaxRequest.onreadystatechange功能之前执行,触发设置workRegionCode