简体   繁体   English

从回调函数调用其他函数

[英]calling other function from callback function

I like to have some help... being stuck with this for a while. 我希望获得一些帮助...会被卡住一段时间。

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

}

Frankly said, I'm very novice on Cordova and javascript callback concept. 坦率地说,我对Cordova和javascript回调概念非常陌生。 I really do appreciate help from you guys. 我真的很感谢你们的帮助。

You are calling a function and defining it later. 您正在调用一个函数并在以后定义它。 Defining otherFunction(count) before finalizeUpdateInventory() should remove the error. finalizeUpdateInventory() otherFunction(count)之前定义otherFunction(count)应该可以消除该错误。 So the modified code becomes: 因此,修改后的代码变为:

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

I finally found the error inside otherFunction. 我终于在otherFunction中发现了错误。 It seems that otherFunction is calling html element that not exist in that same page. 似乎otherFunction正在调用同一页面中不存在的html元素。 This html element exist in other pages that use otherFunction. 此html元素存在于其他使用otherFunction的页面中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM