Reputation: 29
Is autoPlayTimeout not intended to stop autoplay:true? Because no matter what I try Owl Carousel never stops.
$('.owl-carousel').owlCarousel({
items: 1,
autoplay:true,
autoPlaySpeed:500,
autoPlayTimeout:5000,
autoPlayHoverPause:true,
loop:true,
Surely someone else wanted something other than loop forever, since that is annoying.
Upvotes: 1
Views: 1302
Reputation: 1396
Here's what I wound up doing to limit the carousel cycles to a specific number.
var itemCount = e.item.count; // Total number of items
var currentItem = e.item.index + 1; // Current item index (1-based)
if (itemCount === currentItem) { currentLoop += 1; }
var maxCycles = this.core.settings.maxCarouselCycles;
// Check if the current loop exceeds the maximum allowed loops
if (currentLoop >= maxCycles) {
setTimeout(() => {
// Set it back to the first slide + stop all autoplay activity. Wait 1.5 seconds because it does it too fast otherwise.
owl.trigger('to.owl.carousel', 0);
playerCore.DisableCarouselAutoplay();
}, 1500);
}
I did this in the "Animate" Object and defined my code within the event handler named "translated.owl.carousel" which occurs after a slide transition. You'll just have to declare your max Number of cycles either globally in the owl.carousel.js file or you can do what I did and drive it off of a setting that you pass in when establishing the owl object.
If you'd like to do that you'll just have to do the following:
this.InitCarouselEvents = function (enableAutoScroll, scrollDelay, maxCycles) {
owl = $('.owl-carousel').owlCarousel({
autoplay: enableAutoScroll,
autoplayTimeout: scrollDelay,
// Custom parameter
maxCarouselCycles:maxCycles
});
Then you can define a default as well here:
Autoplay.Defaults = {
autoplay: false,
autoplayTimeout: 5000,
autoplayHoverPause: false,
autoplaySpeed: false,
maxCarouselCycles: 3
};
Additionally... if you want to ensure autoplay stops altogether, om certain situations it seems that you need to set a couple of the settings to false. I added these two lines to Autoplay.prototype.stop
//MODIFIED BY RS: Added by to address autoplay not stopping on Mobile + Fullsreen when "TouchDrag"" was initiated
this._core.settings.autoplay = false;
this._core.settings.autoplayHoverPause = false;
Upvotes: 0
Reputation: 19
You can combine the 'stop.owl.autoplay' trigger with a timer and stop the carousel after going through each slide once, or twice or as much as you want.
In order to loop it twice and stop, you can use the following configurations:
Now with these options, your slider will loop forever, showing one slide for 1 second and move to the next one.
In order to stop it after going through each slide twice, you can use the 'onInitialized' event provided by owl and call a function when the slider is initialized. Let's say you have 3 slides in total, so if you want to stop it after the second loop, you have to wait 6000ms (6 seconds).
So in the callback function, you can set a timer to stop the slider at the 6th second. Example carousel :
let $carousel = $('.carousel');
let itemsCount = Number($('.carousel').attr("data-items"));
let autoplayTimeout = 1000;
let loops = 2;
$carousel.owlCarousel({
loop: true,
autoplay: true,
nav: true,
dots: true,
items: 1,
autoplayTimeout: autoplayTimeout,
// Stops after the second cycle
onInitialized : stopTimer({
itemsCount,
autoplayTimeout,
loops
}),
});
function stopTimer(params) {
setTimeout(
function() {
$carousel.trigger('stop.owl.autoplay');
},
params.itemsCount * Number(params.autoplayTimeout) * params.loops
);
}
You can find more events, provided by owl HERE. Hope this will help! :)
Upvotes: 1