Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery run click function every x seconds

Tags:

jquery

I have the following jQuery

<script type="text/javascript">
        jQuery(function(){              
            jQuery('.link1').click(function(){
                jQuery('.hide-div').hide();
                jQuery('.toggle1').show();
                jQuery('#arrow').css({top: '0px'});
            });

            jQuery('.link2').click(function(){
                jQuery('.hide-div').hide();
                jQuery('.toggle2').show();
                jQuery('#arrow').css({top: '42px'});
            });

            jQuery('.link3').click(function(){
                jQuery('.hide-div').hide();
                jQuery('.toggle3').show();
                jQuery('#arrow').css({top: '84px'});
            });

            jQuery('.link4').click(function(){
                jQuery('.hide-div').hide();
                jQuery('.toggle4').show();
                jQuery('#arrow').css({top: '125px'});
            });

            jQuery('.link5').click(function(){
                jQuery('.hide-div').hide();
                jQuery('.toggle5').show();
                jQuery('#arrow').css({top: '166px'});
            });

            jQuery('.link6').click(function(){
                jQuery('.hide-div').hide();
                jQuery('.toggle6').show();
                jQuery('#arrow').css({top: '207px'});
            });
        });


        jQuery(function(){
            jQuery("#toggle-links ul > li > a").click(function(e){
                e.preventDefault();
            jQuery("#toggle-links ul > li > a").addClass("selected").not(this).removeClass("selected");
            });
        });
    </script>

And need to add a function which will run the click functions in order link1, link2, link3... every 3 seconds until it gets to link6 then it will loop back to link1 and if a user was to hover over a div with the id #holder the function would stop running until mouseout. I am a bit stumped over as to do this, any ideas?

like image 993
user786731 Avatar asked Aug 23 '11 16:08

user786731


People also ask

How do you call a function every second in jQuery?

Answer: Use the JavaScript setInterval() method You can use the JavaScript setInterval() method to execute a function repeatedly after a certain time period. The setInterval() method requires two parameters first one is typically a function or an expression and the other is time delay in milliseconds.

How do you call a function after every second?

The setInterval() method calls a function at specified intervals (in milliseconds). The setInterval() method continues calling the function until clearInterval() is called, or the window is closed. 1 second = 1000 milliseconds.

How do I call a function after 2 seconds in jQuery?

To call a jQuery function after a certain delay, use the siteTimeout() method. Here, jQuery fadeOut() function is called after some seconds.


1 Answers

Try:

var interval = null;

jQuery(function(){
  interval = setInterval(callFunc, 3000);
});

function callFunc(){
  jQuery('.link1, .link2, .link3').trigger('click');
}

Any time you can stop auto clicks by calling:

clearInterval(interval);

To call them in order, you can modify your code like this:

        jQuery('.link1').click(function(){
            jQuery('.hide-div').hide();
            jQuery('.toggle1').show();
            jQuery('#arrow').css({top: '0px'});

            // click link2
            jQuery('.link2').trigger('click');
        });

        jQuery('.link2').click(function(){
            jQuery('.hide-div').hide();
            jQuery('.toggle2').show();
            jQuery('#arrow').css({top: '42px'});

            // click link3
            jQuery('.link3').trigger('click');
        });
like image 154
Sarfraz Avatar answered Nov 03 '22 00:11

Sarfraz