angular4中定时执行某函数

updateResCzbt(){
        this.elasticSearchService.updateResCzbt();
        var that = this;
        that.interval = setInterval(function(){
            that.getUpdateStatus()
        },3000);
    }

    async getUpdateStatus(){
        let res = await this.elasticSearchService.getUpdateStatus();
        if (res.content) {
            this.alertService.ShowInfo(res.content);
        }
    }

注意:1.用that代替this,this容易找不到指代的是谁。

           2.最后记得在组件的destory方法里面清除定时器,不然切换页面后发现之前的定时器还在执行,如:

ngOnDestroy() {
        clearInterval(this.interval);
    }

 

你可能感兴趣的:(angular)