所以,我有以下代码:
function sortVitalsByDate(tx, results){
vitals_counter = 0;
showLoader();
if(results.rows.length > 0){
for (var i=0;i<results.rows.length;i++){
vitals_counter += 1;
var record_id = results.rows.item(i).id;
readData('clinicalObservation', record_id, populateVitalsDateList);//calls readData function, uses populateVitalsDateList as the success callback (below)
//hideVitalsLoader();
}
console.log("< === sqlHostingArray == "+ sqlHostingArray); //sqlHostingArray is blank
}else{
$("#vitals-measures-content").html('<div class="no-info">No Information Entered</div>');
hideVitalsLoader();
}
console.log(arguments.callee.name + ", # rows: " + results.rows.length);
}
function populateVitalsDateList(tx, results){
var listItems = "";
for (var i=0;i<results.rows.length;i++){
var data_element = results.rows.item(i).data_element;
var heading;
var label;
var listItem;
var value;
var bpsVal;
switch(data_element){
case 'clinicalObservationDate':
label = null;
value = null;
listItem = null;
var dateObj = new Date(parseInt(results.rows.item(i).field_value,10));
//console.log("< == dateObj is " + results.rows.item(i).field_value);
//heading = formatDate(dateObj);
// v1.1.1 CRM date sorage callback
storeDateTemporarily(dateObj);
heading = decrypt(results.rows.item(i).field_label);
sqlHostingArray.push(heading);
break;
case 'bloodPressureSystolic':
label = "Blood Pressure";
value = decrypt(results.rows.item(i).field_value);
bpsVal = value;
break;
case 'bloodPressureDiastolic':
label = "Blood Pressure";
value = decrypt(results.rows.item(i).field_value);
if (value != "" && value != null && value != undefined){
listItem = addDateLI(results.rows.item(i).record_id, label, bpsVal + "/" + value);
}else{
listItem = "";
}
break;
case 'pulse':
label = "Pulse";
value = decrypt(results.rows.item(i).field_value);
if (value != "" && value != null && value != undefined){
listItem = addDateLI(results.rows.item(i).record_id, label, value + " bpm");
}else{
listItem = "";
}
break;
case 'respRateValue':
label = "Respiratory Rate";
value = decrypt(results.rows.item(i).field_value);
if (value != "" && value != null && value != undefined){
listItem = addDateLI(results.rows.item(i).record_id, label, value + " bpm");
}else{
listItem = "";
}
break;
case 'heightValue1':
label = "Height";
value = decrypt(results.rows.item(i).field_value);
if (value != "" && value != null && value != undefined){
listItem = addDateLI(results.rows.item(i).record_id, label, value + " cm");
}else{
listItem = "";
}
break;
case 'weightValue':
label = "Weight";
value = decrypt(results.rows.item(i).field_value);
if (value != "" && value != null && value != undefined){
listItem = addDateLI(results.rows.item(i).record_id, label, value + " kg");
}else{
listItem = "";
}
break;
case 'bmi':
label = "BMI";
value = decrypt(results.rows.item(i).field_value);
if (value != "" && value != null && value != undefined){
listItem = addDateLI(results.rows.item(i).record_id, label, value);
}else{
listItem = "";
}
break;
case 'tempBody':
label = "Temperature";
value = decrypt(results.rows.item(i).field_value);
if (value != "" && value != null && value != undefined){
listItem = addDateLI(results.rows.item(i).record_id, label, value + " °C");
}else{
listItem = "";
}
break;
default:
label = null;
listItem = null;
break;
}
if (label != null && label != "" && label != undefined && listItem != null && listItem != "" && listItem != undefined){
listItems += listItem;
}
//console.log(arguments.callee.name + ", label: " + label + ", listItem: " + listItem + ", data_element: " + data_element);
vitals_counter -= 1;
hideVitalsLoader();
}
}
这一切都很好,除了行console.log("< === sqlHostingArray == " + sqlHostingArray);
告诉我在完成后直接在它上面的for循环之前调用console.log。意思是,推送到sqlHostingArray
的变量没有在我需要使用它们时定义,即使它应该在之后执行。如果在循环内部调用变量,它工作正常,但我必须在循环完成后执行它。有谁知道修复它的方法?
修改
好的,所以它以异步方式执行....如何保持我需要在for循环后执行所需的部分,等待循环结束。