0
我有一个JavaScript类,必须经常发布事件。代码如下所示:访问setTimeout函数中的类变量
class TimerService {
constructor(BroadcastService) {
this.Broadcast = BroadcastService;
this.timeout;
this.warningTimer;
this.startTimer();
}
startTimer() {
clearTimeout(this.timeout);
clearTimeout(this.warningTimer);
this.timeout = setTimeout(this.startWarning, 30000);
}
startWarning() {
this.Broadcast.send("timeout-warning");
this.warningTimer = setTimeout(this.logout, 60000);
}
logout() {
this.Broadcast.send("session-expired");
}
}
export default { service: TimerService, name: "TimerService" };
的问题是,在setTimeout的calllback功能外,this
范围点window
所以我不能够访问this.Broadcast
。什么是最好的方式来构建这个,所以我可以访问我需要的一切?