簡體   English   中英

無法讀取未定義的屬性“ [功能]”

[英]Cannot read property '[function]' of undefined

我在home.ts類中使用Ionic 2.2.1編寫Typescript。

我收到錯誤: TypeError: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()中調用了相同的函數

當您將this.timerFunc傳遞給setTimeout函數時,您將失去this上下文。 要正確綁定范圍,可以使用.bind(this)

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

或者使用箭頭功能來自動綁定this

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM