Reset setinterval on click

后端 未结 4 708
栀梦
栀梦 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:27

    The above code really helped. Thanks Nick. I made a few minor adjustments so that once you clicked the function: the timer would stop, the function would execute, and then the timer initiates and resumes regular intervals.

    make sure you re-assign the var int to the new interval you create within the click function.

    var int = setInterval(change_slide, 5000);
    $("#div").click(function() {
        console.log('clicked');
      clearInterval(int);
      setTimeout(function() {
      int = setInterval(change_slide, 5000);
      });
    });
    

提交回复
热议问题