从回调函数调用其他函数

calling other function from callback function

我想得到一些帮助...被困了一段时间。

var inventoryID = '123456';

function pickupFail(){
    db = window.openDatabase("myInvetory", "1.0", "myInvetory", 200000);
    db.transaction(queryUpdateInventory, dbError);
}

function queryUpdateInventory(tx){
    var sql = "SELECT inventoryCount FROM Inventory WHERE inventoryID = ?";
    tx.executeSql(sql, [inventoryID], finalizeUpdateInventory, dbError);
}

function finalizeUpdateInventory(tx, results){
   ....
   var inventoryCount = 0;
   var inventory = results.rows.item(0);
   ....
   inventoryCount = inventory.count;
   ....
   ....
   otherFunction(inventoryCount); // CALLING THIS PRODUCE CALLBACK ERROR
   ....
}

function otherFunction(count,...){
   ....
   //THIS IS LENGTHILY FUNCTION AND BEING USED BY OTHER FUNCTION AS WELL

}

坦率地说,我对 Cordova 和 javascript 回调概念非常新手。非常感谢你们的帮助。

您正在调用函数并稍后定义它。在 finalizeUpdateInventory() 之前定义 otherFunction(count) 应该可以消除错误。 所以修改后的代码变成:

var inventoryID = '123456';

function pickupFail(){
    db = window.openDatabase("myInvetory", "1.0", "myInvetory", 200000);
    db.transaction(queryUpdateInventory, dbError);
}

function queryUpdateInventory(tx){
    var sql = "SELECT inventoryCount FROM Inventory WHERE inventoryID = ?";
    tx.executeSql(sql, [inventoryID], finalizeUpdateInventory, dbError);
}

function otherFunction(count,...){
   ....
   //THIS IS LENGTHILY FUNCTION AND BEING USED BY OTHER FUNCTION AS WELL

}

function finalizeUpdateInventory(tx, results){
   ....
   var inventoryCount = 0;
   var inventory = results.rows.item(0);
   ....
   inventoryCount = inventory.count;
   ....
   ....
   otherFunction(inventoryCount); // CALLING THIS PRODUCE CALLBACK ERROR
   ....
}

终于在otherFunction里面找到了错误。似乎 otherFunction 正在调用同一页面中不存在的 html 元素。此 html 元素存在于使用 otherFunction 的其他页面中。