我不明白为什么我的全局变量 workRegionCode
设置不正确。
在函数getWorkRegion()
中,在获得ajax回调后,它尝试设置workRegionCode
全局变量。 (在函数 setFirstIndexWorkRegionCode()
内部)。
setFirstIndexWorkRegionCode()
中的警报输出预期值,例如 401 或 123 等。
但是当调用 getMachines()
时,全局变量 workRegionCode
未定义:(
这个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);
}
最佳答案
AJAX 中的“A”代表异步。这意味着 AJAX 调用被触发,其余代码继续执行。它不等待响应。这将是一个同步调用。如果 getMachines() 依赖于 getWorkRegions() 的响应,您应该这样做:
window.onload = function() {
getWorkRegions();
}
getWorkRegions() {
...
if (ajaxRequest.status == 200) {
...
var jsonOut = JSON.parse(ajaxRequest.responseText);
setFirstIndexWorkRegionCode();
addWorkRegionToDropdown();
getMachines();
...
}
...
}
关于javascript - 为什么没有设置全局变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5114567/