Reset setinterval on click

后端 未结 4 710
栀梦
栀梦 2021-02-09 12:48

I\'ve written this simple Carousel. At the moment I\'m using setInterval to run my nextSlide function at certain intervals. I want to defer the timer from running when a user cl

4条回答
  •  渐次进展
    2021-02-09 13:20

    You can save the return value of setInterval in a variable to refer to it later - that way you can cancel it if you need to, or restart it.

    See this MDC article for more details.

    The basics are:

    intervalID = setInterval(flashText, 1000);
    
    //do something...
    
    clearInterval(intervalID);
    

提交回复
热议问题