我有一個Object,InsertDB
,它包含多個函數。我想一個接一個地執行。執行多個功能
只有執行功能2 InsertDB.addNot();
當函數1 InsertDB.addBk();
已經完全結束循環,插入記錄,等等。
// Object with 7 functions to be called. Each performs a loop and insert records into IndexedDB
InsertDB = {
addBk: function(Object) {
if (Object.hasOwnProperty("Books")) {
for (var i = 0, j = Object["Books"].length; i < j; i++) {
server.Books.add({
title: Object["Books"][i].id,
content: Object["Books"][i]
});
}
}
},
addNot: function(Object) {
if (Object.hasOwnProperty("Notifications")) {
for (var i = 0, j = Object["Notifications"].length; i < j; i++) {
server.Notifications.add({
content: Object["Notifications"][i]
});
}
}
} etc...
}
//On Ajax success event, run above functions one after the other as described above.
Synchronize = {
Start: function(){
return $.ajax({
......
success: function(data){
var Object = $.parseJSON(data);
InsertDB.addBk(Object);
InsertDB.addNot(Object);
InsertDB.addUser(Object);
InsertDB.addHistory(Object); ect...
}
}};
Synchornize.Start();
使用回調(從異步調用)執行相繼。在function1的回調中執行function2,依此類推。 –