2011-06-15 3 views
0

내 프로그램에 다음 코드 행이 있습니다. javascript 클래스에서 setTimeout을 구현하고 싶습니다. 나는 "this"를 저장하려고 시도했지만 나에게 도움이되지 않는 것 같습니다. 이 클래스 로직은 입력이 0이면 출력 0을 표시하지만 입력이 1이면 이미 지정된 타이머를 시작하고 타이머가 끝난 후 출력에 ondelay 타이머를 제공합니다.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 2 개 매개 변수,하지 3. 무시 공급하는 세 번째 매개 변수를 수신

(의견과 this page 참조) 셋째 : 재귀 적으로 Tick을 x 밀리 초 단위로 시간 제한을 사용하여 호출하려는 경우을 실행하는 것과 같습니다.간격은 x 밀리 초입니다. Javascript에는 setInterval이 포함되어 있으므로 다음과 같이 사용해야합니다. setInterval(thisObj.Tick, 1000);

+0

최신 브라우저 (IE 제외)는'setTimeout()'에 추가 매개 변수를 지원합니다. – Alnitak

+0

나는 당신의 요점 # 1도 잘못되었다고 생각한다 - 그가 항상 thisObj.Tick()이라고 부르는 것처럼'thisObj'는 다음 번에'thisObj'에 다시 저장된 컨텍스트 (ie'this')가된다. 약. – Alnitak

+0

@Alnitak : 네가 맞아 대답을 편집했다. 'thisObj'에 대해서는 확실하지 않습니다. From MDN : *'setTimeout()'에 의해 실행되는 코드는 호출 된 함수와는 별도의 실행 컨텍스트에서 실행됩니다. 결과적으로, 호출 된 함수에 대한'this' 키워드는 window (또는 global) 객체로 설정 될 것이고, setTimeout을 호출 한 함수의이 값과 같지 않을 것입니다. * – KooiInc

관련 문제