Tom
Tom

Reputation: 12998

javascript - ignore timeout on click

I have a slideshow which works fine, leaving a 3 second gap between images.

I also have a set of dynamically generated links which when clicked on, the next image is corresponds to that link.

What I want to do is skip the 3 second time out when one of these links is clicked - then restart the timeout after the image is changed.

Code below:

$(document).ready(function() {

var images=new Array();
var totalimages=6;
var totallinks=totalimages;
var nextimage=2;

while (totallinks>0) {
    $(".quicklinks").prepend("<a href='#' class='"+(parseInt(totallinks))+"' onclick='return false'>"+(parseInt(totallinks))+"</a> ");
    totallinks--;
}

function runSlides() {
    if(runSlides.opt) {
        setTimeout(doSlideshow,3000);
    }
}

function doSlideshow()
    {
        if($('.myImage').length!=0)
            $('.myImage').fadeOut(500,function(){slideshowFadeIn();$(this).remove();});
        else
            slideshowFadeIn();
    }

function slideshowFadeIn()
    {
        if(nextimage>=images.length) 
            nextimage=1;

        $('.container').prepend($('<img class="myImage" src="'+images[nextimage]+'" style="display:none;">').fadeIn(500,function() {
            runSlides();
            nextimage++;
        }));
    }

if(runSlides.opt) {} else {
    images=[];
    totalimages=6;
    while (totalimages>0) {
        images[totalimages]='/images/properties/images/BK-0'+parseInt(totalimages)+'.jpg';
        totalimages--;
    }
    runSlides.opt = true;
    runSlides();
}

$(".quicklinks a").live('click', function() {
    nextimage=$(this).attr("class");
});


});

Upvotes: 0

Views: 844

Answers (1)

Van Coding
Van Coding

Reputation: 24554

You can stop a timeout using this code:

var t = setTimeout(myFunction,3000);
clearTimeout(t);

Using this, you can abort your timeout when the user clicks the button and call the function directly. Then you can restart the timeout.

Hope this helps.

Upvotes: 4

Related Questions