使用jQuery递延,并在一个循环中异步SQLite数据库进行查询 [英] Using jQuery deferred and done on asynchronous SQLite database queries in a loop

查看:89
本文介绍了使用jQuery递延,并在一个循环中异步SQLite数据库进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在那里插入查询被放置在一个循环内的应用程序:

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  
});

但我似乎无法弄清楚如何当所有在$。每个刀片的()循环完成后运行一个函数或警报或东西。我有一个想法,我可以使用jQuery的递延/做/希望,但是不能把它应用到这个问题。

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.

任何想法?

推荐答案

我试图找到一个重复的和我的惊讶我没有,所以这里有云:

I tried to find a duplicate and to my surprise I didn't, so here goes:

您通常会使用 $。当,以汇聚多个承诺,如果它是你可以使用一个动态的数字 $。when.apply 这需要一个数组。一般 - 这看起来是这样的:

You'd generally use $.when in order to aggregate multiple promises, if it's a dynamic number you could use $.when.apply which takes an array. Generally - this looks something like:

var promises = [p1, p2, p3, ...];
$.when.apply($, promises).then(function(){
      // all done,
      arguments; // contains all the results in an array-like
});

在您的例子 - 这应该是这样的:

In your example - this would look like this:

var promises = [];
var transactionDone = $.Deferred(); // represent the transaction
promises.push(transactionDone);
db.transaction(function(ctx) {
    ctx.executeSql("DELETE from table",  [], function(x,y){
        $.each(result, function(i, val) {
            var d = $.Deferred();
            db.transaction(function(ctx) {
                ctx.executeSql("INSERT INTO table(value1, value2) VALUES('"+val.value1+"','"+val.value2+"')", []);
            }, d.reject, d.resolve); // resolve/reject when done/fail
            promises.push(d); // keep track of it.
        });
    });
}, transactionDone.reject, transactionDone.resolve);


$.when.apply($, promises).then(function(){
      // transaction itself is done and all inserts are complete
});

这应该给你如何进行一个总的想法,但我个人倒在一个较低的水平promisify。请<一href=\"http://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises\">refer就如何这样做更多的细节这个答案。

This should give you a general idea of how to proceed but personally I'd promisify at a lower level. Please refer to this answer on more details on how to do so.

这篇关于使用jQuery递延,并在一个循环中异步SQLite数据库进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆