2011-06-15 23 views
0

私のプログラムには次のコード行があります。私はjavascriptクラスでsetTimeoutを実装したいと思います。私は "これ"を保存しようとしましたが、私のためにうまくいかなかったようです。 このクラスのロジックでは、入力が0の場合は出力0が表示されますが、入力が1の場合は既に指定されているタイマーが開始され、タイマーが終了するとオンデレータイマーが出力されます。javascriptクラスのsetTimeoutコールを再帰的に呼び出す

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

this.timerId = setTimeout ... – mplungjan

+0

thisObj.Tick()メソッドが呼び出されていないことを意味しますか? –

+0

はいthisObj.Tick()メソッドが呼び出されていません – Yashprit

答えて

0

thisObjthis.Tickは親への参照ではありません。だから、あなたのコードがコンストラクタ関数の一部であれば、変数を設定すれば十分です(var thisObj = this)そうでなければ、あなたが持っている場所から提示したコードからは分かりませんそれを設定するために、コンストラクタ関数ではそれは次のようになります。。

var Parent = function(){ 
    var thisObj = this; 
    /* ... */ 
    this.Tick = { /*... */ }; 
    /* etc. */ 
} 

第二に、 setTimeoutはあなたが供給しない3.三番目のパラメータは無視され、2つのパラメータを受信し (コメントやthis pageを参照)

3番目:再帰的にTickをxミリ秒ごとにタイムアウトを使用して呼び出す場合は、と同じですの間隔はxミリ秒です。 JavascriptにはsetIntervalが含まれていますので、これを使用する必要があります:setInterval(thisObj.Tick, 1000);

+0

新しいブラウザ(IEを除く)は 'setTimeout()'の追加パラメータをサポートしています。 – Alnitak

+0

私はあなたのポイント#1も間違っていると思う - 彼は常にthisObj.Tick()と呼んでいるので、 'thisObj'は次回の' thisObj'に再び保存されるコンテキスト(すなわち 'this')になりますまわり。 – Alnitak

+0

@Alnitak:そうです、答えを編集しました。 'thisObj'に関しては、わからない。 MDNから:* 'setTimeout()'によって実行されるコードは、呼び出された関数とは別の実行コンテキストで実行されます。結果として、呼び出された関数の 'this'キーワードはウィンドウ(またはグローバル)オブジェクトに設定され、setTimeoutを呼び出した関数のこの値と同じになりません。* – KooiInc

関連する問題