2010-02-15 224 views
3

有没有办法让jquery在mouseout事件触发之前等待一段时间?Jquery - 延迟鼠标事件

它现在发射得太早了,我更愿意等待500ms后再评估鼠标。下面我使用的代码示例。

$('.under-construction',this).bind({ 
    mousemove: function(e) { 
     setToolTipPosition(this,e); 
     css({'cursor' : 'crosshair' }); 
    }, 
    mouseover: function() { 
     $c('show!'); 
     showUnderConstruction(); 
    }, 
    mouseout: function() { 
     $c('hide!'); 
     hideUnderConstruction(); 
    }, 
    click: function() { 
     return false; 
    } 
}); 

是否有jQuery的方式来做到这一点,或者我将不得不自己做?

+1

showUnderConstruction()延迟(500)。 hideUnderConstruction()。delay(500); – ant

回答

8

mouseout内部的逻辑拆分为另一个函数。在mouseout甚至用setTimeout("myMouseOut", 500)调用此函数。如果用户移动到新元素中,您可以将mouseover事件与clearTimeout()组合起来以重置计时器。

5

你总是可以将你的逻辑包装在setTimeout()函数中。

mouseout: function() { 
    setTimeout(function(){ 
    $c('hide!'); 
    hideUnderConstruction(); 
    }, 500); 
}