ren*_*thy 3 javascript single-page-application angular
我需要每 30 秒刷新一次角度组件的数据。我使用简单setInterval:
this.interval = setInterval(() => {
this.refresh(); // api call
}, 10000);
Run Code Online (Sandbox Code Playgroud)
但是,这是不正确的,因为即使我导航到另一个“页面”(在 Angular SPA 中,所有内容都是一页,所以它实际上不是另一页),每 30 秒刷新一次。
仅在特定页面/组件上每 30 秒刷新一次数据的正确方法是什么?
您可以OnDestroy在组件的生命周期钩子上销毁间隔。
使用 clearInterval(this.interval)
ngOnDestroy() {
if (this.interval) {
clearInterval(this.interval);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6105 次 |
| 最近记录: |