I'm working on an autoscroll script, I'm using a function in jquery to move to the clicked element on the menu. And I'm using a setInterval to click every item on the menu. Til now everything works ok. But the problem comes when I need to stop the setInterval, I mean, stop the setInterval if a user click one of the elements.

I have two issues or questions for yous:

  1. Is this the correct way to do an autoscroll?
  2. If is not, I'd really appreciate if you give me a little clue to do it. If it is the correct way, how to know if the click event was triggered by the user or by the script?

Well. Thanks for reading. The code I'm using is the next and, as I said, is working ok to loop trought the slider. THANKS A LOT. Just as a sidenote: for a client petition, I cannot use a jquery plugin to create the slider, so I have to do it manually.

$('.slider-controls a').live('click',function(e){
            moveSlider($(this).attr('href'),e);

            return false;
        });

        function moveSlider(toDiv,elem){
            var nextPos = $(toDiv).position().top;
            $('.slider-total').animate({
                top: -nextPos
            }, 700);

            $('.slider-total div').removeClass('activo');
            $(toDiv).addClass('activo');
        }

        var autoSlider = setInterval(function(){
            var slideNext;

            if ( !($('.slider-total .activo').next().attr('id')) ){
                slideNext = '#' + $('.slider-total .post:first').attr('id');
            } else {
                slideNext = '#' + $('.slider-total .activo').next().attr('id');
            }

            $('a[href='+slideNext+']').click();
        },4000);
有帮助吗?

解决方案

You can clear interval by calling clearInterval() passing the id of the interval, returned from setInterval() function. In your case it will be:

clearInterval(autoSlider);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top