James Hobden
James Hobden

Reputation: 5

Adjust javascript slider so that first slide has a different timeout to the rest of the slides

I am trying to make the first slide in the sequence timeout at a different rate to the rest of the slides.

I think I need something like this - If slide == slide.1, setTimeout to 30000 otherwise 1500

This is what I have already:

var slideIndex = 1;
var timer = null;
showSlides(slideIndex);

function plusSlides(n) {
    clearTimeout(timer);
    showSlides(slideIndex += n);
}

function currentSlide(n) {
    clearTimeout(timer);
    showSlides(slideIndex = n);
}

function showSlides(n) {
    var i;
    var slides = document.getElementsByClassName("mySlides");
    var dots = document.getElementsByClassName("dot");

    if (n == undefined) { n = ++slideIndex }
    if (n > slides.length) { slideIndex = 1 }
    if (n < 1) { slideIndex = slides.length }
    for (i = 0; i < slides.length; i++) {
        slides[i].style.display = "none";
    }
    for (i = 0; i < dots.length; i++) {
        dots[i].className = dots[i].className.replace(" active", "");
    }
    slides[slideIndex - 1].style.display = "block";
    if ('undefined' !== typeof dots[slideIndex - 1]) {
        dots[slideIndex - 1].className += " active";
    }
    timer = setTimeout(showSlides, 15000);
}
function pauseShow() {
    clearTimeout(timer);
}

Upvotes: 0

Views: 187

Answers (1)

user9648914
user9648914

Reputation:


you did much code for less function i guess.
When i see it correctly your slide Index indicates to which slide your function should slide to.
In this case you are right, you will need:

if(slideIndex == 1) setTimeout(showSlides, 3000);
else setTimeout(showSlides, 1500); 

At the last index you also need a reset of your slideIndex to 1 again.
Its just another if clause again.

I would also suggest you search for "mySlides" not in your recursive function. You could just search for it before declaring the function and use the global var. This prevents searching in the whole dom every time you want to slide.

Upvotes: 1

Related Questions