Ionic 4 使用 setInterval 函数尝试每秒调用一个函数,但该函数从未调用过

Ionic 4 using setInterval function trying to call a function every second but the function never called

我正在使用 Ionic 4。在其中一个页面中,我尝试调用一个函数来每秒更新一个离子范围值。代码编译没有问题,但永远不会调用 changeMark 函数。

Html 代码:

 <ion-range color="light" pin="false"  min="0" max="100" [(ngModel)]="sliderValue">

 </ion-range>  

这是我的 TS 代码:

  export class UpdatePlayer implements OnInit {

  get_duration_interval: any;
  sliderValue : any  ;

  constructor(public modalController: ModalController) { }

 ngOnInit() { }

 play() {
   ....

   this.get_duration_interval = setInterval(this.changeMark(), 1000);
 }

 changeMark() : any {
this.sliderValue = (this.audio.currentTime/this.maxDuration) * 100  ;
    this.currentDuration = this.fmtMSS(parseInt(this.audio.currentTime));
    console.log('playing time Value : '  + this.audio.currentTime );
 }

 }

我找到了解决办法。通过更改一行代码。 这是代码更改,现在 changeMark 函数按预期每秒触发一次。

 From : this.get_duration_interval = setInterval(this.changeMark(), 1000);


    To:  this.get_duration_interval= setInterval(()=> { this.changeMark() }, 1000);