Javascript 完成时带有回调的db事务

Javascript 完成时带有回调的db事务,javascript,Javascript,我试图在db事务中编写一个函数。我需要的是事务完成时的回调 到目前为止,我掌握的代码是: myAp.getData = function(refresh, callback) { var db = appData.db; // Get API key myAp.getSettings(function(key) { ApiKey = key; var addedOn = new Date(); // Get API url var apiUrl = wi

我试图在db事务中编写一个函数。我需要的是事务完成时的回调

到目前为止,我掌握的代码是:

myAp.getData = function(refresh, callback) {

var db = appData.db;

// Get API key
myAp.getSettings(function(key) {

    ApiKey = key;

    var addedOn = new Date();

    // Get API url
    var apiUrl = window.localStorage.getItem("api_url");

    $.ajax({
        type: "POST",
        url: "http://" + apiUrl + "/get-json-data.php",
        crossDomain: true,
        data: JSON.stringify({ id: 1234, device_id: MyDevice.uuid, api_key: ApiKey }),
        success: function(data){

            // Check for valid login
            if (typeof data.message == 'undefined') {

                // Relations
                if (typeof data.relation != 'undefined') { 

                    $.each( data.relation, function( key, val ) {

                        var id = key;
                        var name = data.relation[key]['name'];
                        var addedOn = new Date();

                        db.transaction(function(tx) {

                            console.log(name); /* This line shows up after the 'Relations ready' message */

                            tx.executeSql("INSERT INTO relations(id, name, added_on) VALUES (?,?,?)",
                              [id, 
                               name,
                               addedOn]);
                        });


                    });

                    console.log('Relations ready'); /* This line show up first before the record are saved to the database */

                }

                // Next loop


            } else {

                if(data.message != 'No records found') {
                    alert(data.message);
                }
            }   

            callback();
        },
        failure: function() {
            alert('failure');
        },
        error: function() {
            alert('error');
        },
    }); 

});
}

正如您在评论中看到的,首先显示的是关系就绪,而不是关系名称

你知道怎么解决这个问题吗