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?
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.
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.
To call a jQuery function after a certain delay, use the siteTimeout() method. Here, jQuery fadeOut() function is called after some seconds.
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');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With