开发者

Need help with db.transaction in javascript function.... is there a delay?

开发者 https://www.devze.com 2023-03-12 08:22 出处:网络
I have this code in a JavaScript file: function getProvincialStandardForRace(seedAge, seedGender, seedCourse, seedDistance, seedEvent) {

I have this code in a JavaScript file:

function getProvincialStandardForRace(seedAge, seedGender, seedCourse, seedDistance, seedEvent) {

    var seedAge;
    var seedGender;
    var seedCourse;
    var seedDistance;
    var seedEvent;
    // Declare SQL database variables
    var shortName = 'SwimChamp';
    var version = '1.0';
    var displayName = 'SwimChamp';
    var maxSize = 5*1024*1024;
    var db = openDatabase(shortName, version, displayName, maxSize);

    db.transaction (
        function(transaction) {
            transaction.executeSql(
                'SELECT * FROM standards WHERE (age=? AND gender=? AND course=? AND distance=? AND event=?);',
    开发者_如何转开发            [seedAge, seedGender, seedCourse, seedDistance, seedEvent],
                function(transaction, result) {
                    localStorage.standardRetrieved = result.rows.item(0)['provstandard'];   
                    console.log(localStorage.standardRetrieved);
                }
            );
        }
    );
    console.log(localStorage.standardRetrieved);
}

Why does the second console.log, the one outside the db.transaction, actually execute before the console.log inside my success function?

I'm completely perplexed. It looks like there's a delay in executing the db.transaction, and code after the db.transaction executes first, then the db.transaction finishes last.


Your function inside the executeSql function is a callback for when the sql executes and that the executeSql function is asynchronous. So the executeSql function returns immediately, the second console.log function is called, executeSql function finishes and calls the first console.log.

0

精彩评论

暂无评论...
验证码 换一张
取 消