当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > angular2组件中定时刷新并清除定时器的实例讲解

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

2018年09月21日  | 移动技术网IT编程  | 我要评论

实例如下:

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网