How to clear interval and set it again?

前端 未结 5 1148
离开以前
离开以前 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 03:54

    Here is utility object that takes a callback and interval which you can use to start and stop an interval.

       //simple utility object to start and stop an interval       
        var IntervalUtil = function(functionCall, interval){
    
    
            var intervalObj = 0,
                INTERVAL    = interval;
    
            var callback = functionCall;
    
            function startTimer(){
                console.log('start timer', intervalObj)
                if(intervalObj === 0){
                    intervalObj = setInterval(callback, INTERVAL)
                }
            }   
    
            function stopTimer(){
                clearInterval(intervalObj);
                intervalObj = 0;
                console.log('timer stopped', intervalObj);
            }
    
            return {
                startTimer : startTimer,
                stopTimer  : stopTimer
            }
    
        };  
    

    *You would use it like so *

    
    
         
        
    
        Timer : setInterval() 
    
    
        
    
    
    
    
    
    

    Set Interval Code

    Start Timer

提交回复
热议问题