在javascript函数中需要db.transaction的帮助....有延迟吗?

时间:2011-06-11 03:46:31

标签: javascript mysql

我在JavaScript文件中有这段代码:

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

为什么第二个console.log({1}}之外的db.transaction实际上是在我的成功函数中console.log之前执行的?

我完全感到困惑。看起来执行db.transaction会有延迟,而db.transaction首先执行代码,然后db.transaction最后完成代码。

1 个答案:

答案 0 :(得分:0)

您在executeSql函数中的函数是sql执行时的回调函数,而executeSql函数是异步的。所以executeSql函数立即返回,调用第二个console.log函数,executeSql函数完成并调用第一个console.log。