- Don't continually re-query the DOM. Cache your control and slide elements in a variable yourso you don't need to spend time re-querying for them.
- You don't need to maintain state around
clickedSlidercondition. Consider usingclearInterval()instead as more "proper" way to manage interval functionality. - Consider breaking apart your
autoSlide()function into it's key parts - one function for managing the current slide index and another for actually performing the action of making slide at given index "active". This gives you more code re-use in giving the to give capability to jump directly to a slide at a given index as is needed in your control click handler. - Do you really aneed data properties for your controls and slides? If there is a 1-to-1 relationship between the slides and the controls and their ordering in DOM is the same, you should be able to know the control at index X relates to the slide at index X.
$(document).ready(function(){ var $testimonialSlides = $('.testimonial-slider-slides a'); var $testimonialControls = $('.testimonial-slider-controls a'); var slideCount = $testimonialSlides.length; var slideIndex = 0; function activateSlide(idx) { $testimonialSlides .removeClass('active') .eq(idx).addClass('active'); $testimonialControls .removeClass('active') .eq(idx).addClass('active'); } function autoSlide(){ slideIndex++; if (slideIndex === slideCount) { slideIndex = 0; } activateSlide(slideIndex); } var autoslideIntervalautoSlideInterval = window.setInterval(autoslideautoSlide, 2000); $testimonialControls.click(function(e){ e.preventDefault(); window.clearInterval(autoslideIntervalautoSlideInterval); slideIndex = $testimonialControls.index(this); activateSlide(slideIndex); }); }); Why have superfluous comments? If youyour code is well-written with meaningful function and variablesvariable names (I think you do a good job of this), then you should not need such comments.