angular2组件中定时刷新并清除定时器

来源:互联网 发布:淘宝店铺升天猫费用 编辑:程序博客网 时间:2024/06/06 23:25

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);

}

}

}


阅读全文
5 0