views:

145

answers:

1

With a few of your help I was able to get the jquery I wanted to work flawlessly, except for one thing.. the animation doesn't stop when i click on the buttons.

Scenario:
I have an Image, and 3 buttons underneath labeled "1","2", and "3". The jquery will automate the click function every 4500ms and switch from 1 to 2, then 2 to 3 and continuously loop. However the problem is, if I manually click on a 1,2,3 button the animation does not stop.

Any ideas how I could accomplish this?

jQuery:

var tabs;
var len;
var index = 1;
var robot;

function automate() {
    tabs.eq((index%len)).trigger('click');
    index++;
}

robot = setInterval(automate, 5500);

jQuery(document).ready(function(){
    jQuery(".imgs").hide();
 jQuery(".img_selection a").click(function(){
        stringref = this.href.split('#')[1];
        $(".img_selection a[id$=_on]").removeAttr('id');
        this.id = this.className + "_on";
        jQuery('.imgs').hide();
        if (jQuery.browser.msie && jQuery.browser.version.substr(0,3) == "6.0") {
            jQuery('.imgs#' + stringref).show();
        } else
            jQuery('.imgs#' + stringref).fadeIn();
        return false;
    });
    $('.img_selection a').removeAttr('id').eq(0).trigger('click');
    tabs = jQuery(".img_selection a");
    len = tabs.size();
}); 

I tried adding the below code, with a lot of help from this website, but to no avail..

CODE:

jQuery(document).ready(function(){
    jQuery(".imgs").hide().click(function(){
  clearInterval(robot);
 });

HTML:

<!-- TOP IMAGE ROTATION -->
    <div id="upper_image">
        <div id="img1" class="imgs">
         <p><img src="images/top_image.jpg" width="900" height="250" alt="" border="0" /></p>
        </div>
         <div id="img2" class="imgs">
         <p><img src="images/top_image2.jpg" width="900" height="250" alt="" border="0" /></p>
        </div>
        <div id="img3" class="imgs">
         <p><img src="images/top_image3.jpg" width="900" height="250" alt="" border="0" /></p>
        </div>
        </div>
<!-- / TOP IMAGE ROTATION -->
<!-- TOP IMAGE SELECTION -->
     <ul class="img_selection">
            <li><a id="img1_on" class="img1" href="#img1">1</a></li>
            <li><a class="img2" href="#img2">2</a></li>
            <li><a class="img3" href="#img3">3</a></li>
        </ul>

<!-- / TOP IMAGE SELECTION -->
A: 

Without seeing the HTML I can't be certian but it looks like you are adding the click event to the image and not the button.

jQuery(document).ready(function(){ 
    jQuery(".img_selection a").click(function(){ 
        clearInterval(robot); 
    });

If that doesn't work you could always create a local variable to pause the animation.

var pause = false;

jQuery(document).ready(function(){    
    jQuery(".imgs").hide().click(function(){    
        pause = true;   
    }); 

function automate() {  
    if (pause) return;
    tabs.eq((index%len)).trigger('click');     
    index++;     
}  
Shaun Humphries
I think my problem is the automate trigger is click, so if I add ClearInterval(robot); in a click(function) it's going to kill the process.I tried your 2nd piece of code as well, but that stopped the entire process as well.. I believe because the trigger runs click, then automatically sets pause=true, regardless.Hrmm..
iamtheratio
If you can add the HTML it will be a lot clearer what the issue is.
Shaun Humphries
HTML has been added. :)
iamtheratio
The click event in the first code block should work. Can you verify if you've tried it.
Shaun Humphries
It works, but it's ran on pageload due to the automate function, so it runs clearinterval(robot); before you manually click on the button. So therefore the slideshow doesn't work anymore
iamtheratio