我在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
最后完成代码。
答案 0 :(得分:0)
您在executeSql函数中的函数是sql执行时的回调函数,而executeSql函数是异步的。所以executeSql函数立即返回,调用第二个console.log函数,executeSql函数完成并调用第一个console.log。