更新时间:2023-09-29 22:46:04
您需要在组件的ngOnDestroy
挂钩方法中使用clearInterval
方法.为此,您需要通过setInterval
方法保存返回的值.
You need to use clearInterval
method for this within the ngOnDestroy
hook method of your component. For this you need to save the returned value by the setInterval
method.
以下是示例:
ngOnInit() {
this.battleInit();
this.id = setInterval(() => {
this.battleInit();
}, 5000);
}
ngOnDestroy() {
if (this.id) {
clearInterval(this.id);
}
}