clearInterval() not working in Angular 5 app

Rahul picture Rahul · Jan 26, 2018 · Viewed 8.7k times · Source

setInterval() working fine for me and timer starts, but clearInterval() doesn't stop timer when counter value reached to 100. It running continuously. Any help appreciated.

Below is my component code -

export class AppComponent {
  counter=0;
  progressInterval;

  ngOnInit(){
    this.progressInterval=setInterval(()=>{
      this.counter=this.counter+10;
      if(this.counter>=100){        
          clearInterval(this.progressInterval);
      }
    },200);
  }
}

Below is my component HTML code -

<p style="margin:20px;">
    <ngb-progressbar
      type="warning"
      [value]="counter"
      [striped]="true"
      [animated]="true"
    >{{counter}}</ngb-progressbar>
  </p>

and here is screenshot which shows progressbar -

Screenshot

Thanks

Answer

Rahul picture Rahul · Jan 26, 2018

Issue got fix for me. I forgot to import "clearInterval" from "timers" module. Now i updated like below and it worked now.

import { 
  setInterval,
  clearInterval
} from 'timers';

Thanks for all for helping me on this.

Thanks