2017-09-09 6 views
1

저는 home.ts 클래스에 Ionic 2.2.1이있는 Typescript로 쓰고 있습니다.정의되지 않은 '[function]'속성을 읽을 수 없습니다.

나는 오류를 얻을 : 형식 오류 : this.GetLocalInformation이 실행에 기능 없습니다. 여기

는 두 가지 기능이 상호 작용하는 방법입니다 : 내가 전화 때문에

ngOnInit() { 
this.GetLocalInformation(); //sets this.school to the school 
this.fullSchedule = this.schedProvider.GetCorrespondingSchool(this.school); 
this.fullSchoolName = this.schedProvider.GetFullSchoolName(this.school); 


this.timerFunc(); //Timer Function -- This method is in a loop with itself 
} 

timerFunc() { //Self Updating Timer Method 
this.GetLocalInformation(); 
this.periods = this.schedProvider.GetPeriod(this.school); 
this.timeInSeconds = ((this.periods[0].h * 3600) + (this.periods[0].m * 60)) - (new Date().getHours() * 3600 + new Date().getMinutes() * 60) - new Date().getSeconds(); //Calculate time difference 
this.currentP = this.periods[0].title; 
this.nextP = this.periods[1].title; 

var dt = Date.now() - this.expected; // the drift (positive for overshooting) 
if (dt > this.interval) { 
    // something really bad happened. Maybe the browser (tab) was inactive? 
    // possibly special handling to avoid futile "catch up" run 
} 
//Set Variables 

//Update Timer 
//this.timer.timer.secondsRemaining = +this.timeInSeconds; 
this.timer.updateTimer(this.timeInSeconds); 
this.expected += this.interval; 
setTimeout(this.timerFunc, Math.max(0, this.interval - dt)); // take into account drift 
} 

내가 이해되지 않는다 timerFunc()의 첫 번째 줄에 오류 메시지가 나타납니다 ngOnInit에서 완벽하게 정상적으로 동일한 기능 ()

답변

4

this.timerFuncsetTimeout 함수로 전달하면 this 컨텍스트가 손실됩니다.

setTimeout(this.timerFunc.bind(this), Math.max(0, this.interval - dt) 

을 또는 자동 바인드 this에 화살표 기능을 사용 : 제대로 범위를 결합하려면 .bind(this)을 사용할 수 있습니다

setTimeout(() => this.timerFunc(), Math.max(0, this.interval - dt)) 
+1

당신의 대답은 완벽하고 쉽게 이해했다. 고맙습니다! 매우 감사 –

관련 문제