Angular8 中使用setInterval定时器,在定时器方法里面修改参数无效问题

  • 一开始的方法使用的方法 使用 setInterval(this.runTime, 1000);调用,无论如何runTime的值都不变。
  1. import {Component, Input, OnInit} from '@angular/core';
  2. import {BlogService} from '../../common-share/api/blog/blog.service';
  3. import {NzNotificationService} from 'ng-zorro-antd';
  4. @Component({
  5. selector: 'app-home',
  6. templateUrl: './home.component.html',
  7. styleUrls: ['./home.component.scss']
  8. })
  9. export class HomeComponent implements OnInit {
  10. runTime = 0;
  11. constructor(private blogService: BlogService,
  12. private notification: NzNotificationService) {
  13. }
  14. ngOnInit() {
  15. setInterval(this.runTime, 1000);
  16. }
  17. runTime() {
  18. this.runTime += 1;
  19. }
  20. }

修改后:
setInterval(() => this.runTime += 1, 1000);