成功回调不在webSQL事务中调用另一个函数

时间:2015-12-30 06:33:32

标签: javascript callback

请参阅以下代码。我在setQuestion()的sucessCallBack中调用db.transaction,但收到此错误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();
      },
});
});

1 个答案:

答案 0 :(得分:0)

这是因为成功回调在其他范围内被调用,而不是你想象的。要解决此问题,您可以尝试在回调时使用闭包:

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