微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

递归javascript类中的setTimeout调用

如何解决递归javascript类中的setTimeout调用

| 我的程序中有这些代码行。我想在javascript类中实现setTimeout。我尝试存储\“ this \”,但似乎对我来说没有用。 在此类逻辑中,如果输入为0,则它​​将显示输出0,但是如果输入为1,它将启动一个已经指定的计时器,在该计时器完成后,它将为输出提供一个ondelay计时器。
ONDClass = function(){
    this.id;
    this.input;
    this.source;
    this.target;
    this.timer;
        var timerValue;
        this.TimerID;
        this.TotalSeconds;
        var thisObj = this;
        var c = 0;
        this.setID = function(id){
              this.id = id;
              timerValue = this.id + \"C\";
        }

        this.getID = function(){
              return this.id;
        }

        this.setTimer = function(timer){
              this.timer = timer
        }

        this.getTimer = function(timer){
              return this.timer;
        }

    this.UIelementTaget = function(target){
        this.target = target;
    }

        this.UIelementSource = function(source){
        this.source = source;
    }

    this.setInput = function(input){
        this.input = input;
    }

    this.getInput = function(){
        return this.input
    }

        this.UpdateTimer = function(){
            console.log(c);
            document.getElementById(timerValue).innerHTML = c;
        }

        this.createTimer = function(timerValue,time){
              this.TimerID = document.getElementById(timerValue);
              this.TotalSeconds = time;
             // this.UpdateTimer();

              setTimeout(function() { 
                    document.getElementById(timerValue).innerHTML = c;
                    console.log(c);
                    thisObj.Tick();
              },1000)
        }

        this.Tick = function(){
              c++
              //this.UpdateTimer();

              if(c < this.TotalSeconds){
                    setTimeout(function() { 
                                document.getElementById(timerValue).innerHTML = c;
                                console.log(c);
                                thisObj.Tick();
                          },1000)
              } else {
                    return this.input;
              }
        }


        this.getoutput = function(){
              if(this.input == 0){
                    var htmltimer = \"<div id = \" + timerValue + \" class = \'timerValue\'>\" + this.getTimer() + \"</div>\";
                    $(\"#\" + this.id).append(htmltimer);
                    return this.input;
              } else {
                    var htmltimer = \"<div id = \" + timerValue + \" class = \'timerValue\'></div>\";
                    $(\"#\" + this.id).append(htmltimer);
                    this.createTimer(timerValue,this.getTimer());
                    return this.input;
              }
    }

       this.getUISourceElement = function(){
              if(this.source == undefined)
                   return false;
              else
                    return true;
        }

        this.addExTradiv = function(){
        var divinput = this.id +\"I\";
              var divoutput = this.id +\"O\";
              var htmlinput = \"<div id = \" + divinput + \" class = \'inputBoxTimer\'>\" + this.getinput()+ \"</div>\";
              var htmloutput = \"<div id = \'\" + divoutput + \"\' class = \'outputBoxTimer\'>\" + this.getoutput() + \"</div>\";
              $(\"#\" + this.id).append(htmloutput);
              $(\"#\" + this.id).append(htmlinput);

        }
}
问题出在setTimeout函数上。它不是每次都在打电话,它给我的输出天气是1还是0     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。