How to clear interval and set it again?

前端 未结 5 1129
离开以前
离开以前 2020-12-30 03:15

This is what i am trying to accomplish: when the last slide is reached fadeOut last slide and then fadeIn first slide, and then clearInterval (everything works with th

5条回答
  •  长发绾君心
    2020-12-30 04:06

    After clear an interval you need to start it again with setInterval().

    It would be better to make function for your setInterval()

    var intervalID = null;
    
    function intervalManager(flag, animate, time) {
       if(flag)
         intervalID =  setInterval(animate, time);
       else
         clearInterval(intervalID);
    }
    

    Here flag is a boolean variable with value true/ false. true will execute setInterval() and false will clearInterval();

    Now you can use above function as you need.

    For example:

    intervalManager(true, animate, 300);  // for setInterval
    
    intervalManager(false);  // for clearInterval
    

提交回复
热议问题