【发布时间】:2020-10-22 17:22:51
【问题描述】:
我有一个 IONIC 4 应用程序,我需要每 20 秒调用一次 api,如果用户移动到其他页面需要停止调用该 api。我能够每 20 秒进行一次 api 调用,但当我移动到其他页面时无法停止它。下面是我的代码,请问我哪里出错了?
rateTimer:any;
constructor(private loginServiceService: LoginServiceService) {
this.loginServiceService.getappData();
this.rateTimer=setInterval(() => {
this.loginServiceService.getappData();
}, 10000);
}
// When I move to other page, I clear the setInterval from here
ngOnDestroy() {
clearInterval(this.rateTimer);
}
【问题讨论】:
-
您确定调用了 ngOnDestroy 吗?在 ionic 中,您应该使用 Ionic Page Lifecyle ionicframework.com/docs/angular/lifecycle
-
是的 ngOnDestroy 被调用了,我确实被击中了
标签: javascript angular ionic-framework ionic4 angular8