Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用setInterval在angular 4中加载列表时,单击其他链接时也会触发相同的loadlist函数_Javascript_Angular - Fatal编程技术网

Javascript 使用setInterval在angular 4中加载列表时,单击其他链接时也会触发相同的loadlist函数

Javascript 使用setInterval在angular 4中加载列表时,单击其他链接时也会触发相同的loadlist函数,javascript,angular,Javascript,Angular,在angular 4应用程序中,我必须在指定的时间内加载网格。我也使用了setInterval。请参阅下面使用的代码段 间隔时间:任意 ngOnInit(): void { this.loadList(); this.interval = setInterval(() => { this.loadList(); }, 30000); 即使在移动到另一个链接后,loadList也会每30秒执行一次。如果用户在正确的页面上,我必须加载列

在angular 4应用程序中,我必须在指定的时间内加载网格。我也使用了setInterval。请参阅下面使用的代码段

间隔时间:任意

ngOnInit(): void {

    this.loadList();

    this.interval = setInterval(() => {  

        this.loadList();

    }, 30000);  
即使在移动到另一个链接后,loadList也会每30秒执行一次。如果用户在正确的页面上,我必须加载列表。现在它总是开火。如何解决同样的问题


请任何人帮助解决同样的问题

如果您只想在该组件上使用该间隔,可以实现内置的OnDestroy接口并使用如下方法Ngondstroy:

import {OnDestroy} from '@angular/core';
export class yourComponent implements OnDestroy {

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

如果您只希望在该组件上使用该间隔,则可以实现内置的OnDestroy接口,并使用如下方法Ngondstroy:

import {OnDestroy} from '@angular/core';
export class yourComponent implements OnDestroy {

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

您可以删除
ngondestory
上的interval listener(当您导航出该页面组件时),并在再次加载该页面时重新附加它

ngOnDestroy(): void {
   clearInterval(this.interval);
}

您可以删除
ngondestory
上的interval listener(当您导航出该页面组件时),并在再次加载该页面时重新附加它

ngOnDestroy(): void {
   clearInterval(this.interval);
}

您是否只希望它运行一次?如果是这样的话,setTimeout而不是setInterval会更好。是否只希望它运行1次?如果是这样,setTimeout而不是setInterval会更好。