Javascript 无法读取属性';[功能]';未定义的
我在home.ts课上用Ionic 2.2.1打字 我得到错误:TypeError:this.GetLocalInformation不是函数 行刑时 以下是这两个功能如何相互作用:Javascript 无法读取属性';[功能]';未定义的,javascript,typescript,ionic-framework,ionic2,Javascript,Typescript,Ionic Framework,Ionic2,我在home.ts课上用Ionic 2.2.1打字 我得到错误:TypeError:this.GetLocalInformation不是函数 行刑时 以下是这两个功能如何相互作用: ngOnInit() { this.GetLocalInformation(); //sets this.school to the school this.fullSchedule = this.schedProvider.GetCorrespondingSchool(this.school); this.fu
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)
:
或者使用箭头功能自动绑定此
:
setTimeout(this.timerFunc.bind(this), Math.max(0, this.interval - dt)
setTimeout(() => this.timerFunc(), Math.max(0, this.interval - dt))
当您将this.timerFunc
传递到setTimeout
函数时,您将丢失this
上下文。要正确绑定范围,可以使用。绑定(this)
:
或者使用箭头功能自动绑定此
:
setTimeout(this.timerFunc.bind(this), Math.max(0, this.interval - dt)
setTimeout(() => this.timerFunc(), Math.max(0, this.interval - dt))
你的回答很有效,也很容易理解。非常感谢。非常感谢你的回答完美且容易理解。非常感谢。非常感谢