下面是我传递JSON对象的函数。我遍历该JSON并使用每个JSON元素中找到的信息更新本地数据库。问题是我的功能" getAEDFromDB"在每个循环结束之前调用。 我怎样才能确保在循环结束后调用它?
function getAEDFromWeb_callBack(json) {
var hasUpdated = true;
alert("getAEDfromWeb_callback" + JSON.stringify(json));
$.each(JSON.parse(json), function(idx, obj) {
//check if id exists
db.transaction(function (t) {
t.executeSql('SELECT * FROM tbAED WHERE id = ' + obj.id, null, function (t, data) {
if (data.rows.length > 0) {
//exists - therefore update
alert("update AED");
t.executeSql("UPDATE tbAED SET name='" + obj.name + "',address='" + obj.address + "',address2='" + obj.address2 + "',latitude='" + obj.latitude + "',longitude='" + obj.longitude + "',description='" + obj.description + "',photo='" + obj.picture + "',status=" + obj.status + " WHERE id=" + obj.id, [], function (t, data) {
hasUpdated = true;
},
function(t, e) {
alert('1 error table insert settings. ' + e.message);
});
} else {
//doesnt exist therefore insert
alert("insert AED");
t.executeSql("INSERT INTO tbAED (id,name,address,address2,latitude,longitude,description,photo,status) VALUES (" + obj.id + ",'" + obj.name + "','" + obj.address + "','" + obj.address2 + "','" + obj.latitude + "','" + obj.longitude + "','" + obj.description + "','" + obj.picture + "'," + obj.status + ")", [], function (t, data) {
hasUpdated = true;
},
function(t, e) {
alert('2 error table insert settings. ' + e.message);
});
}
},
function(t, e) {
alert('error sql. ' + e.message);
});
});
})
alert("done");
if (hasUpdated==true) {
alert("update settings with date");
db.transaction(function (t) {
t.executeSql("UPDATE tbSettings SET lastupdate = '" + _templastupdate + "' WHERE userid = " + _userid + ";", [], function (t, data) {
_lastupdate = _templastupdate;
},
function(t, e) {
alert('error updating lastupdate settings. ' + e.message);
});
});
}
getAEDFromDB();
}
由于
答案 0 :(得分:0)
db.transaction(function (t) {
$.each(JSON.parse(json), function(idx, obj) {
//check if id exists
t.executeSql('SELECT * FROM tbAED WHERE id = ' + obj.id, null, function (t, data) {
if (data.rows.length > 0) {
//exists - therefore update
alert("update AED");
t.executeSql("UPDATE tbAED SET name='" + obj.name + "',address='" + obj.address + "',address2='" + obj.address2 + "',latitude='" + obj.latitude + "',longitude='" + obj.longitude + "',description='" + obj.description + "',photo='" + obj.picture + "',status=" + obj.status + " WHERE id=" + obj.id, [], function (t, data) {
hasUpdated = true;
},
function(t, e) {
alert('1 error table insert settings. ' + e.message);
});
} else {
//doesnt exist therefore insert
alert("insert AED");
t.executeSql("INSERT INTO tbAED (id,name,address,address2,latitude,longitude,description,photo,status) VALUES (" + obj.id + ",'" + obj.name + "','" + obj.address + "','" + obj.address2 + "','" + obj.latitude + "','" + obj.longitude + "','" + obj.description + "','" + obj.picture + "'," + obj.status + ")", [], function (t, data) {
hasUpdated = true;
},
function(t, e) {
alert('2 error table insert settings. ' + e.message);
});
}
},
function(t, e) {
alert('error sql. ' + e.message);
});
})
}, function(){} /*this is the error callback*/
, function(){
alert("done");
} /*this is the success callback*/
);
注意:我将警报放在交易成功回调中。