Implement the function provided below into your code to enable autoplay:
function initiateSlider() {
if (current == $('.carousel-item').length) {
current = 0
}
setTimeout(() => {
let next = current;
current += 1;
updateSlide(next, current);
initiateSlider();
}, 3000)
}
initiateSlider();
Explanation:
The initiateSlider()
function is triggered on load, and a timeout is initiated once the specified duration is reached. It then executes the designated function to switch the slide, continues the loop by calling itself again, and checks if the value of current
reaches the last index to reset it back to 0
.
Note: You have the flexibility to adjust the timeout value for a faster or slower autoplay. Currently, 3000
translates to 3 seconds
.
Demo
... (Existing HTML content remains the same)