2015-07-28 56 views
0

我必須調用由特定元素上的mouseenter事件觸發的函數。 只有鼠標在特定元素上「站立」超過5秒時,我如何才能觸發mouseenter事件?JavaScript-僅在特定時間後觸發事件mouseenter,

+0

不同事件同樣的問題/溶液:[jQuery的.keyup()延遲(http://stackoverflow.com/questions/1909441/jquery-keyup-delay) – Andreas

+0

也許這有助於:http://stackoverflow.com/questions/2060601/javascript-cant-stop-the-settimeout – connexo

回答

2

如果鼠標離開之前,您可以使用計時器(setTimeout())在5秒鐘後計劃一個函數,那麼我們可以清除該計時器,以便它不會被調用。

var timer; 
 
$('#me').hover(function() { 
 
    timer = setTimeout(function() { 
 
    snippet.log('called after 5000') 
 
    }, 5000); 
 
}, function() { 
 
    //if leaves early then clear the timer 
 
    clearTimeout(timer); 
 
})
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> 
 
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
Some text 
 
<div id="me">hover me for more time</div> 
 
more content

+0

http://jsfiddle.net/arunpjohny/f7bs3x08/ –

+0

我可以使用鼠標輸入嗎? –

+0

@SaritRotshild http://jsfiddle.net/arunpjohny/f7bs3x08/2/ –

相關問題