0

I have the following code which executes some animations etc. when the user hovers over an element, however if they "enter" and "leave" the element with the mouse rapidly and repeatedly it messes up the animations. How can I deactive the mousenter event and then reactivate it once animations have finished?

$(".women .text").on("mouseenter", function(event) {
            event.preventDefault();
            $( ".men .intro" ).hide();
            $('.women').delay(300).animate({width:"80%"});  
            $('.men').delay(300).animate({width:"20%"});    
            $('.men').animate({opacity: 0.5}, 500); 
            $('.women').animate({opacity: 1}, 500);
            $( ".women .intro" ).delay(600).fadeIn(300);
        });

Thanks!

user1937021
  • 10,151
  • 22
  • 81
  • 143

1 Answers1

1

Change the timer to whatever timespan you want

var mouseEntered = false;

$(".women .text").on("mouseenter", function(event) {
            event.preventDefault();

            if(mouseEntered == false)
            {
                mouseEntered = true;
                $( ".men .intro" ).hide();
                $('.women').delay(300).animate({width:"80%"});  
                $('.men').delay(300).animate({width:"20%"});    
                $('.men').animate({opacity: 0.5}, 500); 
                $('.women').animate({opacity: 1}, 500);
                $( ".women .intro" ).delay(600).fadeIn(300);
                window.setTimeout(function() {
                    mouseEntered = false;
                }, 1000);
            }
        });
simple-thomas
  • 671
  • 7
  • 20
  • cool thanks, does just deactivate it for that set amount of time in the timespan then? – user1937021 Feb 28 '13 at 16:23
  • yep, the line inside the function() that converts the mouseEntered to false window.setTimeout(function() { mouseEntered = false; }, 1000); is going to be executed 1 second or 1000 miliseconds after this piece of code is executed so only after a second can the animations start over. – simple-thomas Feb 28 '13 at 16:25
  • can I not use unbind while a function is performed? – user1937021 Feb 28 '13 at 17:03