How to use clearInterval() in Angular 4

后端 未结 2 686
故里飘歌
故里飘歌 2021-02-08 04:09

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

const inter = setInterval(() => {
  // logic resulting in exitCondition
  if(exitCondition) {
    clearInt         


        
相关标签:
2条回答
  • 2021-02-08 04:39

    If you want in same method with some condition

    var a = 1;
        this.interval = setInterval(() => {
          console.log(a++)
          if(a > 30){
            clearInterval(this.interval);
          }
        }, 1000);
    
    0 讨论(0)
  • 2021-02-08 04:49

    You can set like this,

      this.interval = setInterval(() => {
    
      }, 1000);
    

    and clear like this,

    if (this.interval) {
       clearInterval(this.interval);
    }
    
    0 讨论(0)
提交回复
热议问题