gpt4 book ai didi

javascript - 成功回调未调用 webSQL 事务中的另一个函数

转载 作者:行者123 更新时间:2023-12-02 17:29:13 24 4
gpt4 key购买 nike

请看下面的代码。我在 db.transaction 的 sucessCallBack 中调用了 setQuestion(),但收到此错误 Uncaught TypeError: this.setQuestions is not a function

我的代码有什么问题吗?

game.module(
"game.scenes.scene"
)
.require(
"game.assets",
"game.entities.gameObject",
"game.entities.backgroundObject",
"game.entities.animeObject",
"game.entities.starObject",
"game.entities.buttonObject"
).body(function() {
game.SceneScene1 = game.Scene.extend({
loaded: function() {
this.get_difficulty_level();
},
setQuestions: function() {
//some code
},
get_difficulty_level: function(){
var app_id = this.app_id;
var user_id = this.user_id;
db.transaction(function (transaction)
{
transaction.executeSql('SELECT * FROM User_report where app_id="'+app_id+'" and user_id="'+user_id+'" order by id desc limit 1;', [],
function (transaction, result)
{
if (result.rows.length == 0)
{
difficulty_level=2;
}else{
difficulty_level = result.rows.item(0).difficulty;
console.log(result.rows.item(0));
}
});
},this.errorHandler,this.successDiffi);
},
successDiffi: function(){
this.setQuestions();
},
});
});

最佳答案

发生这种情况是因为成功回调在您想象的范围之外的范围内被调用。要解决此问题,您可以尝试在回调中使用闭包:

game.module(
"game.scenes.scene"
)
.require(
"game.assets",
"game.entities.gameObject",
"game.entities.backgroundObject",
"game.entities.animeObject",
"game.entities.starObject",
"game.entities.buttonObject"
).body(function() {
game.SceneScene1 = game.Scene.extend(
new function ()
{
this.loaded = function() {
this.get_difficulty_level();
};
this.setQuestions= function() {
//some code
};
this.get_difficulty_level= function(){
var app_id = this.app_id;
var user_id = this.user_id;
db.transaction(function (transaction)
{
transaction.executeSql('SELECT * FROM User_report where app_id="'+app_id+'" and user_id="'+user_id+'" order by id desc limit 1;', [],
function (transaction, result)
{
if (result.rows.length == 0)
{
difficulty_level=2;
}else{
difficulty_level = result.rows.item(0).difficulty;
console.log(result.rows.item(0));
}
});
},this.errorHandler,this.successDiffi);
};
var _this = this;
this.successDiffi= function(){
_this.setQuestions();
};
});
});

关于javascript - 成功回调未调用 webSQL 事务中的另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34524201/

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