delay() or timeout with stop()?

前端 未结 4 571
生来不讨喜
生来不讨喜 2020-12-14 17:47
$(\'.file a\').live(\'mouseenter\', function() {
    $(\'#download\').stop(true, true).fadeIn(\'fast\');
}).live(\'mouseleave\', function() {
    $(\'#download\').st         


        
4条回答
  •  一个人的身影
    2020-12-14 18:32

    You need to use setTimeout() in this case because of how .delay() works (and your inability to cancel it).

    $('.file a').live('mouseenter', function() {
      $.data(this, 'timer', setTimeout(function() {
          $('#download').stop(true, true).fadeIn('fast');
      }, 1000));
    }).live('mouseleave', function() {
      clearTimeout($.data(this, 'timer'));
      $('#download').stop(true, true).fadeOut('fast');
    });
    

    You can give it a try here.

    If you use .delay() it'll dequeue the next animation for the element, regardless of if you cleared that queue earlier. So you need a timeout that you can cancel, which the above does by manually calling setTimeout() and storing the result with $.data() so you can clear it later, via clearTimeout().

提交回复
热议问题