网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/5/8 浏览:3 次 )

实例如下:

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

上一篇:JavaScript使用prototype原型实现的封装继承多态示例
下一篇:解决Angular4项目部署到服务器上刷新404的问题
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网