无法在函数上设置间隔
Can't set Interval on function
这是我调用的函数:
refreshServices() {
this.services = this.monitorService.getServices(); }
我调用构造函数,像这样:
constructor(private localNotifications: LocalNotifications,
public monitorService: MonitorService) {
this.refreshServices(); }
这很好用,但是当我这样做时:
constructor(private localNotifications: LocalNotifications,
public monitorService: MonitorService) {
setInterval(this.refreshServices,100000); }
不起作用...这是控制台中的错误消息:
ERROR TypeError: Cannot read property 'getServices' of undefined
那么,有什么想法吗?
问题是 setInterval
在另一个上下文中调用传递的函数,其中函数内部的 this
指的是全局对象 (window
) 而不是 class setInterval
被称为。这意味着当这一行被执行时
this.monitorService.getServices();
等于以下
window.monitorService.getServices();
因为 window
没有 monitorService
属性 初始化你有那个错误。要解决此问题,您需要将函数的上下文绑定到当前 class
//this code ensures that "this" inside "refreshServices" will refer to the class instance
setInterval(this.refreshServices.bind(this),100000);
另一种可能的解决方案是使用箭头函数,它使用封闭范围的上下文
refreshServices = () => {
this.services = this.monitorService.getServices();
}
您必须使用箭头功能来解决您的问题。
看来您需要以下代码:
refressToken() {
this.checkRefreshTokenThread = setInterval(() => {
const expiredTime = this.authStore.getExpiredTimeLocalStorage();
const nowTime = String(new Date().getTime());
const nowTimeJava = Number(nowTime.substr(0, 10));
this.timeOfRefresh = (expiredTime - nowTimeJava) < CONSTANT._9MINUTES;
this.isExpired = expiredTime < nowTimeJava;
if (this.isExpired) {
this.authenticationService.logout();
this.router.navigate(['/login']);
}else if (this.timeOfRefresh && !this.authStore.getIsReadyLocalStorage()) {
this.authStore.setIsReadyLocalStorage('true');
}
}, CONSTANT._1MINUTE);
}
从服务中获取数据后,您应该清除间隔,如下所示:
clearIntervalRefreshToken() {
clearInterval(this.checkRefreshTokenThread);
}
希望对你有用!
这是我调用的函数:
refreshServices() {
this.services = this.monitorService.getServices(); }
我调用构造函数,像这样:
constructor(private localNotifications: LocalNotifications,
public monitorService: MonitorService) {
this.refreshServices(); }
这很好用,但是当我这样做时:
constructor(private localNotifications: LocalNotifications,
public monitorService: MonitorService) {
setInterval(this.refreshServices,100000); }
不起作用...这是控制台中的错误消息:
ERROR TypeError: Cannot read property 'getServices' of undefined
那么,有什么想法吗?
问题是 setInterval
在另一个上下文中调用传递的函数,其中函数内部的 this
指的是全局对象 (window
) 而不是 class setInterval
被称为。这意味着当这一行被执行时
this.monitorService.getServices();
等于以下
window.monitorService.getServices();
因为 window
没有 monitorService
属性 初始化你有那个错误。要解决此问题,您需要将函数的上下文绑定到当前 class
//this code ensures that "this" inside "refreshServices" will refer to the class instance
setInterval(this.refreshServices.bind(this),100000);
另一种可能的解决方案是使用箭头函数,它使用封闭范围的上下文
refreshServices = () => {
this.services = this.monitorService.getServices();
}
您必须使用箭头功能来解决您的问题。 看来您需要以下代码:
refressToken() {
this.checkRefreshTokenThread = setInterval(() => {
const expiredTime = this.authStore.getExpiredTimeLocalStorage();
const nowTime = String(new Date().getTime());
const nowTimeJava = Number(nowTime.substr(0, 10));
this.timeOfRefresh = (expiredTime - nowTimeJava) < CONSTANT._9MINUTES;
this.isExpired = expiredTime < nowTimeJava;
if (this.isExpired) {
this.authenticationService.logout();
this.router.navigate(['/login']);
}else if (this.timeOfRefresh && !this.authStore.getIsReadyLocalStorage()) {
this.authStore.setIsReadyLocalStorage('true');
}
}, CONSTANT._1MINUTE);
}
从服务中获取数据后,您应该清除间隔,如下所示:
clearIntervalRefreshToken() {
clearInterval(this.checkRefreshTokenThread);
}
希望对你有用!