在javascript中获取函数外部的数组值时出现问题?
我已经创建了一个数组,它将存储来自DB的动态值。当我在事务函数内打印数组时,它正在打印。当我试图打印出一面时,我没有得到值。我已经全局声明了数组。问题是什么,我的代码如下在javascript中获取函数外部的数组值时出现问题?,javascript,jquery,arrays,Javascript,Jquery,Arrays,我已经创建了一个数组,它将存储来自DB的动态值。当我在事务函数内打印数组时,它正在打印。当我试图打印出一面时,我没有得到值。我已经全局声明了数组。问题是什么,我的代码如下 function sendCategoryDetails() { var selected_category = $('#select-choice :selected').val(); alert("control : " + selected_category); var mycoodinates
function sendCategoryDetails() {
var selected_category = $('#select-choice :selected').val();
alert("control : " + selected_category);
var mycoodinatesDetails;
var myLocation = new Array();
var db = window.sqlitePlugin.openDatabase({name: "MYDB"});
db.transaction(function (tx) {
tx.executeSql("select Location from Locationlog WHERE Category = '"+selected_category+"';", [],
function (tx, res) {
for (var i = 0; i < res.rows.length; i++) {
myLocation[i] = res.rows.item(i).Location;
}
alert (myLocation +" length : "+ myLocation.length); // Values are printing
});
});
alert (myLocation +" length : "+ myLocation.length); // values are not getting, It showing the length is 0
}
有什么建议吗?tx.executeSql是一个异步函数。警报在函数中起作用,因为这是回调,异步处理已完成。函数完成前触发函数后的警报,因此未定义
解决方案:在回调函数内部执行工作,或者使用作为参数传递的数组调用另一个函数,然后在那里执行工作。tx.executeSql是一个异步函数。警报在函数中起作用,因为这是回调,异步处理已完成。函数完成前触发函数后的警报,因此未定义
解决方案:在回调中执行工作,或者使用作为参数传递的数组调用另一个函数,然后在那里执行工作。欢迎来到异步调用的世界。欢迎来到异步调用的世界。是否可以在另一个tx.executeSql中写入tx.executeSql?我想为数组元素编写查询。我该怎么做?您可以在回调中写入另一个execute。是否可以在另一个tx.executeSql中写入tx.executeSql?我想为数组元素编写查询。我该怎么做?您可以在回调中编写另一个execute。