2013-06-12 47 views
-3

我正在使用jQuery,並且需要檢測鼠標靜態時間超過2秒,然後檢測它是否再次移動。我可能沒有那個好名字,所以我沒有在谷歌上找到任何東西。 謝謝!jQuery:如何檢測鼠標是否靜止

+1

請提供一些證據表明您研究了該主題。 [查詢](http://stackoverflow.com/questions/667555/detecting-idle-time-in-javascript-elegantly)就會產生你的答案。 – kgdesouz

回答

0

As kgdesouz pointed outhere is a simple script using jQuery處理mousemovekeypress事件。如果時間到期,頁面被重新加載。

<script type="text/javascript"> 
idleTime = 0; 
$(document).ready(function() { 
    //Increment the idle time counter every minute. 
    var idleInterval = setInterval("timerIncrement()", 60000); // 1 minute 

    //Zero the idle timer on mouse movement. 
    $(this).mousemove(function (e) { 
     idleTime = 0; 
    }); 
    $(this).keypress(function (e) { 
     idleTime = 0; 
    }); 
}) 
function timerIncrement() { 
    idleTime = idleTime + 1; 
    if (idleTime > 19) { // 20 minutes 
     window.location.reload(); 
    } 
} 
</script> 
+0

非常感謝分享 –

+0

評論部分20分鐘實際上應該是20秒...... – Tzar