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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

friend_ship   2020-05-16 我要评论

实例如下:

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组件中定时刷新并清除定时器的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们