Angular-网页定时刷新
类上方引入“OnInit”、“OnDestroy”
import { OnInit, OnDestroy } from '@angular/core';
类实现“OnInit”、“OnDestroy”
类A implements OnInit, OnDestroy{
......
}
定时器代码方法
ngOnInit() { this.now = new Date(); this.startTime = new Date(); this.runTimer(); }
//定时运行方法 runTimer() { this.nowInterval = setInterval(() => { if (this.startTime) { if (new Date().getTime() - this.startTime.getTime() > 60000) { console.log(this.now); this.getSearch(); } } this.now = new Date(); }, 1000); } ngOnDestroy() { clearInterval(this.nowInterval); }
//查询页面数据
getSearch(event?: LazyLoadEvent) {
this.startTime = new Date();
........
}