Json 在每个循环完成之前调用Jquery函数
下面是一个传递JSON对象的函数。我循环使用该JSON并使用每个JSON元素中的信息更新本地数据库。问题是在每个循环完成之前调用了我的函数getAEDFromDB。 如何确保在循环完成后调用它Json 在每个循环完成之前调用Jquery函数,json,asynchronous,each,synchronous,Json,Asynchronous,Each,Synchronous,下面是一个传递JSON对象的函数。我循环使用该JSON并使用每个JSON元素中的信息更新本地数据库。问题是在每个循环完成之前调用了我的函数getAEDFromDB。 如何确保在循环完成后调用它 function getAEDFromWeb_callBack(json) { var hasUpdated = true; alert("getAEDfromWeb_callback" + JSON.stringify(json)); $.each(
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();
}
谢谢
注意:我将警报放在事务成功回调中。您的意思是,它是在foreach完成之前调用的,还是在all t.executeSql完成之前调用的。注意,executeSql是同步的,因此在db.transaction完成之前将调用Alert'done'。我希望在我的for each之后调用我的alertdone。现在它被称为:非常感谢!你救了我:
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*/
);