clearInterval()不起作用

时间:2017-07-05 09:42:21

标签: angular typescript

我必须在mouseenter事件上启动间隔,我必须在mouseleave上停止它。

当'mousedown'事件启动'setInterval'时,'mouseInterval'没有停止'mouseInterval'由'mouseenter'事件启动的时间间隔正在正常工作

demo.ts

plugins: [function() {
    this.plugin("done", function(stats) {
        //Since webpack dependent Stats.js
        //Just see the API of Stats.js
        //You can do anything with the stats output
        if (stats && stats.hasErrors()) {
            stats.toJson().errors.forEach((err) => {
                console.error(err);
            });
            process.exit(1);
        }
    }
}]

demo.html

  interval_bs:any;
  startInterval(){    
    this.interval_bs=setInterval(()=>{
      if(this.activeIndex<3){
        this.activeIndex+=1;
      }
      else{
        this.activeIndex=0;
      }     
    },2000)   
  }  

  stopInterval(){   
    clearInterval(this.interval_bs);   
  }

1 个答案:

答案 0 :(得分:3)

现在问题已经解决了。声明变量'start_count'并将其初始化为零。 在'(mouseleave)'事件中,将'start_count'重置为零。 现在在'(mouseenter)'事件中增加'start_count'并将条件应用于'setInterval()'方法

interval_bs:any;  
start_count:number=0;
startInterval(){   
    this.start_count+=1;
    if(this.start_count==1){
      this.interval_bs=setInterval(()=>{
        if(this.activeIndex<3){
          this.activeIndex+=1;
        }
        else{
          this.activeIndex=0;
        }        
      },2000)     
    }

  }  

  stopInterval(){   
    this.start_count=0;
    clearInterval(this.interval_bs);   
  }
相关问题