从回调函数调用其他函数

时间:2015-07-10 08:49:53

标签: javascript callback

我希望得到一些帮助......坚持一段时间。

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回调概念非常新手。我非常感谢你们的帮助。

2 个答案:

答案 0 :(得分:0)

您正在调用函数并在以后定义它。在otherFunction(count)之前定义finalizeUpdateInventory()应该会删除错误。 因此修改后的代码变为:

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
   ....
}

答案 1 :(得分:0)

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