setTimeout以递归方式调用javascript类

时间:2011-06-15 05:24:36

标签: javascript

我的程序中有这些代码行。我想在javascript类中实现setTimeout。我试着存储“这个”,但似乎对我不起作用。 在这个类逻辑中,如果输入为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

0 个答案:

没有答案