Reset setinterval on click

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

    See:

    var IntID = setTimer();
    
    function setTimer(){
        i = setInterval(startSlider, 4000);
        return i;
    }
    
    function stopSlider() {
        clearInterval(IntID);
    }
    
    //Restart Timer
    
    // Option 1 make a restartSlider function and call it
    $("#button").click(restartSlider);
    function restartSlider(){
        IntID = setTimer();
    }
    
    //Option 2 create an anonymous function only for that click event.
    $("#button").click(function(){
        IntID = setTimer();
    });
    

提交回复
热议问题