0

I have jQuery carousel that I'm trying to add an interval scroll to without changing the core of the code. I'm doing this by simulating a click on an interval, which runs just fine.

But I do not want the click to happen if the user is hovered over the carousel container.

    var tid = setInterval(carouselClick, 5000);
    var isHovered = jQuery('.jcarousel-clip').is(':hover');
    function carouselClick() {

        if (!isHovered){
            jQuery('#test').html('<h1>'+isHovered+'</h1>');
            jQuery('.jcarousel-next').trigger('click');
        }
    }

I put in the #test bit to check the hover state when the function runs and it always returns false.

Plummer
  • 6,522
  • 13
  • 47
  • 75

1 Answers1

1

You need to put the isHovered inside the click function

var tid = setInterval(carouselClick, 5000);

function carouselClick() {
    var isHovered = jQuery('.jcarousel-clip').is(':hover');
    if (!isHovered){
        jQuery('#test').html('<h1>'+isHovered+'</h1>');
        jQuery('.jcarousel-next').trigger('click');
    }
}

If you leave it outside, then the value of isHovered will always be the same, which is the value when it first runs

Huangism
  • 16,278
  • 7
  • 48
  • 74