简体   繁体   English

我正在使用 setTimeout() 调用刷新 function,但是当我移动到另一个组件时它不会停止调用刷新?

[英]I am using setTimeout() to call a refresh function, but it doesnt stop calling refresh when I move to another component?

I am new to angular, and I am having a function, refresh, which i need to call after every 1 min but when I am into that component it works fine and when i navigate to some other view it is still calling the refresh function, refresh function is called in ngOnInit() and then this setTimeout() function i have created in the refresh() function only.我是 angular 的新手,我有一个 function,刷新,我需要在每 1 分钟后调用一次,但是当我进入该组件时它工作正常,当我导航到其他视图时,它仍在调用刷新 ZC1C125268D18A94, refresh function 在 ngOnInit() 中调用,然后这个 setTimeout() function 我只在 refresh() function 中创建。

ngOnInit(){
 this.refresh();
 }
 ...
 ...

refresh(){
...
...

setTimeout(() => {
  this.refresh();
   },10000) 
 }

You should use setInterval instead of setTimeout and in ngOnDestroy you need to use clearInterval您应该使用setInterval而不是setTimeout并且在 ngOnDestroy 中您需要使用clearInterval

this.timer = setInterval(() => {
   this.refresh();
}, 10000);

Clear interval清除间隔

ngOnDestroy(){
  clearInterval(this.timer);
}

You can create one more property like timeout and update your class like so,您可以再创建一个属性,例如timeout并像这样更新您的 class,

ngOnInit(){
 this.refresh();
 }
 ...
 ...
timeout;
refresh(){
...
...

this.timeout = setTimeout(() => {
  this.refresh();
   },10000) 
 }
ngOnDestroy(){
  clearTimeout(this.timeout);
}

Your class will have to implement OnDestroy interface for this besides OnInit .除了OnInit之外,您的 class 还必须为此实现OnDestroy接口。

Although for your use case, setInterval makes more sense and also you can make a reusable service like IntervalService to encapsulate the whole behaviour.尽管对于您的用例, setInterval更有意义,您也可以制作像IntervalService这样的可重用服务来封装整个行为。

The setInterval bit:- setInterval 位:-

interval;

ngOnInit()
{
this.interval = setInterval(() => {
   this.refresh();
}, 10000);
}
ngOnDestroy(){
  clearInterval(this.interval);
}

refresh(){....}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 停止 setTImeout() 函数刷新倒计时 - Stop setTImeout() function to refresh countdown 为什么当我调用另一个函数刷新窗口时,我的函数没有发送 PUT 请求? - Why is a PUT request not sent by my function when I call another function to refresh the window? 我可以通过调用javascript中的另一个函数来停止循环吗 - Am I able to stop a loop by calling another function in javascript 如何在刷新Javascript时停止此功能 - How can I stop this function on refresh Javascript 内存问题调用setTimeout()到我从中调用的函数? - Memory issues calling setTimeout() to the function I am calling it from? 如何使用javascript中的刷新功能发送数据并使用相同的刷新功能检索其他数据 - How do I send data using a refresh function in javascript and retrieve another data using the same refresh function 我尝试使用settimeout刷新页面时无法正常运行 - settimeout not working properly when i tried to use it to refresh a page 我可以使用 javascript 停止元刷新吗? - Can I stop a meta refresh using javascript? Highcharts:打开新角度组件时如何停止setTimeout函数? - Highcharts: how to stop the function setTimeout when I open new component angular? 使用自定义挂钩我无法刷新/重新呈现组件 - Using a custom hook I am unable to refresh/re-render the component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM