How to use clearInterval () in Angular 4

I am trying to use setInterval in my Angular 4 application.

const inter = setInterval(() => {
  // logic resulting in exitCondition
  if(exitCondition) {
    clearInterval(inter);
  }
}, 1000);

      

This setting works fine in javascript, but clearInterval()

doesn't work in Angular. After doing some research I found a spacing service for Angular 1.x:

https://docs.angularjs.org/api/ng/service/ $ interval

Is there something similar for Angular 4? Or is there a workaround for makeInterval ()?

+3


source to share


1 answer


You can install this,

  this.interval = setInterval(() => {

  }, 1000);

      



and clean it up,

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

      

+7


source







All Articles