Jquery 每个循环完成前调用的函数
Jquery function called before each loop completed
下面是我的一个函数,它传递了一个 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();
}
谢谢
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*/
);
注意:我将警报放在交易成功回调中。
下面是我的一个函数,它传递了一个 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();
}
谢谢
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*/
);
注意:我将警报放在交易成功回调中。