我正在创建一个带有phonegap的应用。我使用一个小jquery和这个插件: html5sql.com / ,用于处理数据库。我的程序以奇怪的顺序运行。我在代码中添加了一些警报,并在我的设备上启动应用程序时显示的数字后面写了一条评论。有谁知道它为什么不从上到下运行?
在global.js
我创建了一个包含三个ID(relID)
的数组var station = function(id) {
this.id = id;
};
var relID=new Array();
relID[0]=2762378;
relID[1]=2746459;
relID[2]=2748307;
在index.js
var app = {
initialize: function() {
this.bindEvents();
alert('vor dbStuff() aufruf');//1-----
var relStations=dbStuff(relID);
alert('nach dbStuff() aufruf');//6-----
//alert(relStations[0].brand); ---UNDEFINED---
},
...phonegap stuff...
在loaddb.js
function dbStuff(relID) {
//NEW ARRAY WHERE ALL STATION OBJECTS WILL BE SAVED
var relStations=[];
alert('dbStuff() neues Array erstellt');//2-----
//OPEN DATABASE AND WRITE DATA INTO
try {
html5sql.openDatabase("stations", "stations", 5 * 1024 * 1024);
alert('open DB');//3-----
$.get('js/db/stations.sql', function(dump) {
alert('sbStuff() dump geladen');//7-----
html5sql.process(
dump,
function() { //Success
alert('dump success');
},
function(error, failingQuery) { //Failure
alert('dump fail'); //14-----
}
);
});
}
catch (error) {
alert("Error: " + error.message);
}
//GET DATA FROM DATABASE AND WRITE IT IN A STATION OBJECT
//(FOR EACH ID IN 'relID' ARRAY)
//FILL 'relStations' ARRAY WITH THE STATION OBJECTS AND RETURN IT
for(var i=0; i<relID.length; i++){
var stationa=new station(relID[i]);
alert('dbStuff()-for neues station Objekt erstellt');//4-----
//ARRAY WITH SQL STATEMENTS
var sqlarray = [
{"sql" : "SELECT latitude FROM stations WHERE id=" + relID[i],
"data" : [],
"success" : function (tx, rs) {stationa.latitude=rs.rows.item(0).latitude;alert('SQL lat');//8-----
}
},
{"sql" : "SELECT longitude FROM stations WHERE id=" + relID[i],
"data" : [],
"success" : function (tx, rs) {stationa.longitude=rs.rows.item(0).longitude;alert('SQL long');//9-----
}
},
{"sql" : "SELECT brand FROM stations WHERE id=" + relID[i],
"data" : [],
"success" : function (tx, rs) {stationa.brand=rs.rows.item(0).brand;alert('SQL brand');//10-----
}
},
{"sql" : "SELECT name FROM stations WHERE id=" + relID[i],
"data" : [],
"success" : function (tx, rs) {stationa.name=rs.rows.item(0).name;alert('SQL name');//11-----
}
},
{"sql" : "SELECT federalstate FROM stations WHERE id=" + relID[i],
"data" : [],
"success" : function (tx, rs) {stationa.federalstate=rs.rows.item(0).federalstate;alert('SQL federalstate');//12-----
}
}
];
try {html5sql.process(
sqlarray,
function () { //Success
//alert('lat: ' + stationa.latitude + ', long: ' + stationa.longitude + ', brand: ' + stationa.brand + ', name:' + stationa.name + ', fedeeralstate:' + stationa.federalstate);
relStations[i]=stationa;
alert('Array füllen');//13-----
//alert(relStations[i].brand);
},
function () { //Failure
alert('SQL FAIL');
});}
catch(error) {
alert("Error: " + error.message);
}
}
alert('return relStations'); //5-----
return relStations;
}
答案 0 :(得分:0)
这可能是因为回调函数必须等待脚本在被调用之前结束。
警告3 之后的get
- 函数将运行,但它的回调函数只有在服务器向get函数返回一些内容时才会运行,这可能需要一些时间。与此同时,函数之后的代码将运行,因为它是一个异步请求
因此,您将看到警报4,5和6 ,之后服务器处理了请求,并且调用了(匿名)回调函数,该警报警报7
其他ajax调用相同。
您可以尝试以下方法。我将一些东西移到函数loadStations
中。
此函数将在get
- 调用的回调函数中调用,但仅在成功的情况下才会调用。
function dbStuff(relID) {
//NEW ARRAY WHERE ALL STATION OBJECTS WILL BE SAVED
var relStations=[];
alert('dbStuff() neues Array erstellt');//2-----
//OPEN DATABASE AND WRITE DATA INTO
try {
html5sql.openDatabase("stations", "stations", 5 * 1024 * 1024);
alert('open DB');//3-----
$.get('js/db/stations.sql', function(dump) {
alert('sbStuff() dump geladen');//7-----
html5sql.process(
dump,
function() { //Success
alert('dump success');
return loadStations(relID);
},
function(error, failingQuery) { //Failure
alert('dump fail'); //14-----
}
);
});
}
catch (error) {
alert("Error: " + error.message);
}
}
function loadStations(relID) {
//GET DATA FROM DATABASE AND WRITE IT IN A STATION OBJECT
//(FOR EACH ID IN 'relID' ARRAY)
//FILL 'relStations' ARRAY WITH THE STATION OBJECTS AND RETURN IT
for(var i=0; i<relID.length; i++){
var stationa=new station(relID[i]);
alert('dbStuff()-for neues station Objekt erstellt');//4-----
//ARRAY WITH SQL STATEMENTS
var sqlarray = [
{"sql" : "SELECT latitude FROM stations WHERE id=" + relID[i],
"data" : [],
"success" : function (tx, rs) {stationa.latitude=rs.rows.item(0).latitude;alert('SQL lat');//8-----
}
},
{"sql" : "SELECT longitude FROM stations WHERE id=" + relID[i],
"data" : [],
"success" : function (tx, rs) {stationa.longitude=rs.rows.item(0).longitude;alert('SQL long');//9-----
}
},
{"sql" : "SELECT brand FROM stations WHERE id=" + relID[i],
"data" : [],
"success" : function (tx, rs) {stationa.brand=rs.rows.item(0).brand;alert('SQL brand');//10-----
}
},
{"sql" : "SELECT name FROM stations WHERE id=" + relID[i],
"data" : [],
"success" : function (tx, rs) {stationa.name=rs.rows.item(0).name;alert('SQL name');//11-----
}
},
{"sql" : "SELECT federalstate FROM stations WHERE id=" + relID[i],
"data" : [],
"success" : function (tx, rs) {stationa.federalstate=rs.rows.item(0).federalstate;alert('SQL federalstate');//12-----
}
}
];
try {html5sql.process(
sqlarray,
function () { //Success
//alert('lat: ' + stationa.latitude + ', long: ' + stationa.longitude + ', brand: ' + stationa.brand + ', name:' + stationa.name + ', fedeeralstate:' + stationa.federalstate);
relStations[i]=stationa;
alert('Array füllen');//13-----
//alert(relStations[i].brand);
},
function () { //Failure
alert('SQL FAIL');
});}
catch(error) {
alert("Error: " + error.message);
}
}
alert('return relStations'); //5-----
return relStations;
}