Angular8 中使用setInterval定时器,在定时器方法里面修改参数无效问题
- 一开始的方法使用的方法 使用 setInterval(this.runTime, 1000);调用,无论如何runTime的值都不变。
import {Component, Input, OnInit} from '@angular/core';
import {BlogService} from '../../common-share/api/blog/blog.service';
import {NzNotificationService} from 'ng-zorro-antd';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
runTime = 0;
constructor(private blogService: BlogService,
private notification: NzNotificationService) {
}
ngOnInit() {
setInterval(this.runTime, 1000);
}
runTime() {
this.runTime += 1;
}
}
修改后:
setInterval(() => this.runTime += 1, 1000);