RiddleMeThis
RiddleMeThis

Reputation: 1345

JavaScript / Jquery clearInterval & setInterval multiple sliders

I have MULTIPLE sliders on one page, everything works except I can't get the set and clear intervals to work properly. I am trying to reset the setInterval when the next and prev functions are called. After I clicked next or prev the clearInterval works, the sliders all stop but weird stuff happens when the new setInterval runs.

I would ideally like to do this for each slider on the page but I can't even globally get it to work (meaning reset all sliders on the page at once).

Thanks for any feedback!

Edit: Including JSFiddle

Edit: I pasted a sample of the JS layout below the actual code

gemco.customSlider = gemco.customSlider || {};

gemco.customSlider = {

nextSlide: function(el, timer){     
    // Set Active Slide
    var parentSlide = el.parent('section');
    var totalSlides = parentSlide.find($('.cs-slide')).length;
    var activeSlide = parentSlide.find($('.cs-slide.active')); 
    var curSlide = activeSlide.data('tab');
    var nextSlideIndex = curSlide + 1;
    if(nextSlideIndex > totalSlides){
        nextSlideIndex = 1;
    }
    var nextSlide = parentSlide.find($('.cs-slide[data-tab="' + nextSlideIndex + '"]'));

    activeSlide.removeClass('active');
    nextSlide.addClass('active');

    // Reset Timer
    gemco.customSlider.resetTimer(timer);
},

prevSlide: function(el, timer){     
    // <Similar to nextSlide>
},

playSlider: function(){
    $('.custom-slider').each(function(){
        var el = $(this).find('.cs-slide.active');
        var parentSlide = el.parents('.custom-slider');
        var totalSlides = parentSlide.find($('.cs-slide')).length;
        var activeSlide = parentSlide.find($('.cs-slide.active')); 
        var curSlide = activeSlide.data('tab');
        var nextSlideIndex = curSlide + 1;
        if(nextSlideIndex > totalSlides){
            nextSlideIndex = 1;
        }
        var nextSlide = parentSlide.find($('.cs-slide[data-tab="' + nextSlideIndex + '"]'));

        activeSlide.removeClass('active');
        nextSlide.addClass('active');

        // Set Active Tab Link
        gemco.customSlider.setActiveTabLink();
    }); 

},

resetTimer: function(timer){
    clearInterval(timer);
    var timer = setInterval(function(){
        gemco.customSlider.playSlider();
    }, 7000);   
},

init: function(){

    var timer = setInterval(function(){
        gemco.customSlider.playSlider();
    }, 7000);   

    $('.cs-next-prev.next').click(function(e){
        timer = timer;
        el = $(this); 
        e.preventDefault();
        gemco.customSlider.nextSlide(el, timer);
    });

    $('.cs-next-prev.prev').click(function(e){
        // <Similar to Above>
    });
}

} // gemco.customSlider 

Here is a sample of the JS overall layout

var obj = obj || {};

obj.customSlider = obj.customSlider || {};
obj.customSlider = {
    nextSlide: function(){      
        // ...
    },

    prevSlide: function(){      
        // ...
    },

    playSlider: function(){
        // ...      
    },

    resetTimer: function(){
        // ...  
    },

    init: function(){
        // Init Functions
    }   
},

obj.sample = obj.sample || {};
obj.sample = {
    init: function() {
        // ...
    }
} 

$(document).ready(function(){
    obj.customSlider.init();
    obj.sample.init();
});

Example of Issue - JSFiddle - Click the next arrow a few times, its like it is creating new timers each time??? If I comment out the new interval inside resetTImer, then hit the next button I can see clearInterval work. Confused.

Upvotes: 0

Views: 294

Answers (1)

Dongdong
Dongdong

Reputation: 2498

make a global timer at the beginning of

gemco.customSlider = (function(){
   var timer;// all functions use this timer only, no more others
   var resetTimer=function(){...};
   var setActiveTabLink=function(){...};
   ....
   return {
    Init: C,//map functions..
    Next: B,
    setActiveTabLink: setActiveTabLink
    ....
   }
})();

and remove var which is in resetTimer and init.

and I don't understand the beginning code:

gemco.customSlider = gemco.customSlider || {};

gemco.customSlider = {//the first row make sure customSlider is not empty, but here overwrites the first row....

More Explain: the reason is your timer is not cleared and it's keep working in background.

this code works: https://jsfiddle.net/z1kg8qdt/29/

changes:

  1. applied Module Design Pattern to your code with a global timer;
  2. removed parameters timer in all functions;
  3. removed this row: gemco.customSlider = gemco.customSlider || {};

JavaScript modules are the most prevalently used design patterns for keeping particular pieces of code independent of other components. This provides loose coupling to support well-structured code.

Upvotes: 1

Related Questions