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();
      ........
  }
 

 

posted on 2019-03-01 14:07  技术高超  阅读(552)  评论(0编辑  收藏  举报