I am making an app where an insert query is placed inside a loop:
db.transaction(function(ctx) {
ctx.executeSql("DELETE from table", [], function(x,y){
$.each(result, function(i, val) {
db.transaction(function(ctx) {
ctx.executeSql("INSERT INTO table(value1, value2) VALUES('"+val.value1+"','"+val.value2+"')", []);
}, function(err){
alert("Error processing SQL: "+err.message);
},function(){
console.log("finished one loop of insert");
});
});
});
}, function(){
//error
}, function(){
//success
console.log("finished syncing");
//this runs before all the inserts as the inserts are seperate queries
});
But I cant seem to figure out how to run a function or alert or something when all of the INSERTS in the $.each() loop are finished. I have an idea I could use jQuery's deferred/done/promise but can't apply it to this problem.
Any ideas?