function buildCarousel(visibleSlides) { $('.amenities-slides').show(); $('.amenities-slides').cycle({ fx: 'carousel', speed: 600, timeout: 0, slides: '.amenities-slide', carouselVisible: visibleSlides, carouselFluid: true, next: "#amenities-next", prev: "#amenities-prev" }); } function initCycle() { var width = $(document).width(); var visibleSlides = 5; if ( width < 480 ) {visibleSlides = 1;} else if(width < 768) {visibleSlides = 3;} else {visibleSlides = 3;} buildCarousel(visibleSlides); } function reinit_cycle() { var width = $(window).width(); var destroyCarousel = function() { // create a function $('.amenities-slides').cycle('destroy'); }; if (width <= 480) { destroyCarousel(); // call the function buildCarousel(1); } else if (width > 480 && width < 768) { destroyCarousel(); buildCarousel(3); } else { destroyCarousel(); buildCarousel(3); } } var reinitTimer; $(window).resize(function() { clearTimeout(reinitTimer); reinitTimer = setTimeout(reinit_cycle, 100); }); $(document).ready(function(){ initCycle(); });