JavaScript 数组在回调方法中使用时不更新
JavaScript Array not updating when using it in callback method
我在 javascript 函数中有一个本地数组。我正在迭代一个 table 有选择(组合框)。对于每个选择,我都对 C# 函数进行 ajax 调用(在循环中)。在收到结果后的回调方法(内部函数)中,我将其添加到本地数组中。然后在遍历 table 行后,我使用该数组调用另一个函数。但是这个数组仍然是空的。
当我更改 table.
的任何组合框的所选项目时,将调用具有本地数组的函数
当我调试它时,我可以看到 ajax 调用的接收项已添加到回调函数中的数组中。但是在循环 table.
之后调用另一个函数时数组仍然为空
如何让它工作,以便在循环 table 后调用函数时数组中充满项目?
这是函数:
function doCourseCheck() {
var errors = [];
var chosenSubjectTypes = []; //this is the array I want to fill
var table = document.getElementById('choiceTable');
var selectId = 0;
var subjectTypeReceived = function (result) {
chosenSubjectTypes.push(result); //array is filled in the callback function (inner function)
}
//looping through the table
for (let j = 0; j < table.rows.length; j++) {
if (j === 0 || j === 3) {
continue;
}
var comboBox = document.getElementById('select_' + selectId);
var subjectName = comboBox.options[comboBox.selectedIndex].text;
if (subjectName !== "") {
//in this function the ajax call is done and the result is given to the callback function
getSubjectType(subjectName, subjectTypeReceived);
}
selectId++;
}
//here the filled array should be used but it stays empty
checkCourseChoices(errors, chosenSubjectTypes);
showErrors(errors);
}
这是完成 ajax 调用的函数:
function getSubjectType(subjectName, callback) {
$.ajax({
type: 'GET',
contentType: 'application/json',
data: { name: subjectName },
dataType: 'json',
url: "/Subjects/Choose?handler=SubjectType",
cache: false,
success: function (result) {
callback(result);
}
});
}
您不能混合同步和异步代码。同步代码将总是完全在异步代码之前执行,无论你想对数组做什么都必须在回调中进行。换句话说,你必须把这个:
checkCourseChoices(errors, chosenSubjectTypes);
showErrors(errors);
subjectTypeReceived
函数内部
我在 javascript 函数中有一个本地数组。我正在迭代一个 table 有选择(组合框)。对于每个选择,我都对 C# 函数进行 ajax 调用(在循环中)。在收到结果后的回调方法(内部函数)中,我将其添加到本地数组中。然后在遍历 table 行后,我使用该数组调用另一个函数。但是这个数组仍然是空的。 当我更改 table.
的任何组合框的所选项目时,将调用具有本地数组的函数当我调试它时,我可以看到 ajax 调用的接收项已添加到回调函数中的数组中。但是在循环 table.
之后调用另一个函数时数组仍然为空如何让它工作,以便在循环 table 后调用函数时数组中充满项目?
这是函数:
function doCourseCheck() {
var errors = [];
var chosenSubjectTypes = []; //this is the array I want to fill
var table = document.getElementById('choiceTable');
var selectId = 0;
var subjectTypeReceived = function (result) {
chosenSubjectTypes.push(result); //array is filled in the callback function (inner function)
}
//looping through the table
for (let j = 0; j < table.rows.length; j++) {
if (j === 0 || j === 3) {
continue;
}
var comboBox = document.getElementById('select_' + selectId);
var subjectName = comboBox.options[comboBox.selectedIndex].text;
if (subjectName !== "") {
//in this function the ajax call is done and the result is given to the callback function
getSubjectType(subjectName, subjectTypeReceived);
}
selectId++;
}
//here the filled array should be used but it stays empty
checkCourseChoices(errors, chosenSubjectTypes);
showErrors(errors);
}
这是完成 ajax 调用的函数:
function getSubjectType(subjectName, callback) {
$.ajax({
type: 'GET',
contentType: 'application/json',
data: { name: subjectName },
dataType: 'json',
url: "/Subjects/Choose?handler=SubjectType",
cache: false,
success: function (result) {
callback(result);
}
});
}
您不能混合同步和异步代码。同步代码将总是完全在异步代码之前执行,无论你想对数组做什么都必须在回调中进行。换句话说,你必须把这个:
checkCourseChoices(errors, chosenSubjectTypes);
showErrors(errors);
subjectTypeReceived
函数内部