angular2组件中定时刷新并清除定时器的实例讲解

2019-11-15,,,,

实例如下:

import { Component,OnInit,ChangeDetectionStrategy,ChangeDetectorRef,OnDestroy} from "@angular/core";

@Component({
changeDetection:ChangeDetectionStrategy.OnPush
})

export class xxxComponent{
private timer;
constructor(private ref : ChangeDetectorRef){
this.timer = setInterval(()=>{
this.ref.detechChanges();//检测变化
},5000)
}
//销毁组件时清除定时器
ngOnDestroy(){
if(this.timer){
clearInterval(this.timer);
}
}
}

以上这篇angular2组件中定时刷新并清除定时器的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持北冥有鱼。

您可能感兴趣的文章:

  • AngualrJs清除定时器遇到的坑
  • AngularJS定时器的使用与移除操作方法【interval与timeout】
  • Angular.js中定时器循环的3种方法总结
  • AngularJS实现页面定时刷新
  • Angular实现的简单定时器功能示例

《angular2组件中定时刷新并清除定时器的实例讲解.doc》

下载本文的Word格式文档,以方便收藏与打印。