我正在尝试在db事务中编写一个函数。我需要的是交易完成后的回调。
到目前为止我的代码:
myAp.getData = function(refresh, callback) {
var db = appData.db;
// Get API key
myAp.getSettings(function(key) {
ApiKey = key;
var addedOn = new Date();
// Get API url
var apiUrl = window.localStorage.getItem("api_url");
$.ajax({
type: "POST",
url: "http://" + apiUrl + "/get-json-data.php",
crossDomain: true,
data: JSON.stringify({ id: 1234, device_id: MyDevice.uuid, api_key: ApiKey }),
success: function(data){
// Check for valid login
if (typeof data.message == 'undefined') {
// Relations
if (typeof data.relation != 'undefined') {
$.each( data.relation, function( key, val ) {
var id = key;
var name = data.relation[key]['name'];
var addedOn = new Date();
db.transaction(function(tx) {
console.log(name); /* This line shows up after the 'Relations ready' message */
tx.executeSql("INSERT INTO relations(id, name, added_on) VALUES (?,?,?)",
[id,
name,
addedOn]);
});
});
console.log('Relations ready'); /* This line show up first before the record are saved to the database */
}
// Next loop
} else {
if(data.message != 'No records found') {
alert(data.message);
}
}
callback();
},
failure: function() {
alert('failure');
},
error: function() {
alert('error');
},
});
});
}
正如您在评论中所看到的那样,关系准备就会首先显示而不是关系名称。
知道如何解决这个问题吗?