简体   繁体   中英

how to set time out for jquery hover function

for example look at these codes please :

HTML:

<div id="hvr">hover me</div>
<div id="box" style="background:#ddd; width:100px; height:50px;"></div>

jQuery:

$(function(){
$("#hvr").hover(function(){

    $("#box").animate({width:'150px'},500);
    more animations/functions here ...

}, function(){

    $("#box").animate({width:'100px'},500);
    more animations/functions here ...
});

when I hover on the text 3 times quickly, the box animates 3 times after each one is finished. how can I set a time out for the hover which will disable hover effect until the first animations/functions [ due to the first hover ] are finished ?

Try this

$(function(){
  $("#hvr").hover(function(){    
      $("#box").stop().animate({width:'150px'},500);   
  }, function(){    
      $("#box").stop().animate({width:'100px'},500);
  });
});

http://api.jquery.com/stop/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM