gpt4 book ai didi

javascript - 处理数据库事务返回

转载 作者:行者123 更新时间:2023-12-03 11:36:16 25 4
gpt4 key购买 nike

这里我有一个函数,它应该返回秒数作为变量 sec,但 return 语句在数据库事务之前运行,所以我一直未定义。我可以做什么来确保事务在返回调用之前完成。类似于 XMLHttpRequestonreadystatechange

function getLevelSeconds (index) {
var sec;
db.transaction(function (tx) {tx.executeSql('SELECT * FROM SPLevelData', [], function (tx, result) {sec=result.rows.item(index).seconds;}, errorCB)}, errorCB);
return sec;
}

最佳答案

实现回调

function getLevelSeconds (index, callback) {
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM SPLevelData', [], function (tx, result) {
sec=result.rows.item(index).seconds;
typeof callback == "function" && callback(sec);
}, errorCB);
}, errorCB);
}

getLevelSeconds(index, function(sec) {
alert(sec);
});

详细信息请参阅JavaScript: 4 asyncronys functions to wait for each other sequentially to finish before continuing?回答。

关于javascript - 处理数据库事务返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26497550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com