【发布时间】:2019-07-20 15:04:11
【问题描述】:
我在角度使用setInterval() 来刷新我的页面,但页面的刷新并没有停止。我也使用过clearInterval(),但它似乎不起作用。请指导并告诉我代码是否有问题或是否有其他停止刷新的方法。
我在ngOnDistroy 中使用了clearInterval(),因为它会影响其他
组件也是如此。但它也没有奏效。
AppComponent.ts
import { Component } from '@angular/core';
export class AppComponent {
intervalId;
i:number=0;
n:number=0;
ngOninit(){
}
refresh(){
this.n++;
alert("**** this is refresh method with "+this.n+" times.****");
this.intervalId=setInterval(()=>{
this.i++;
if(this.i>3){
console.log("we are in stop method");
this.stopRefresh();
}
this.refresh();
},60000);
}
stopRefresh(){
clearInterval(this.intervalId);}
}
AppComponent.html
<div class="row">
<div class="col-xs-12">
<div>
<button (click)="refresh()">Test</button>
</div>
</div>
</div>
我希望代码在变量 'i' 的值达到 3 后停止,但它会继续运行并且永远不会停止。
【问题讨论】:
-
我的回复是否回答了您的问题?
标签: angular setinterval