2013-11-15 48 views
0

我使用下面的javascript代碼來檢測鼠標停頓間隔後500ms檢測鼠標停止時間間隔

document.onmousemove = (
var onmousestop = function() { 
    alert("mouse stopped moving"); 
    }, thread; 

    return function() { 
    clearTimeout(thread); 
    thread = setTimeout(onmousestop, 500); 
    }; 
})(); 

是有辦法檢測鼠標已經停止多長時間移動,如果鼠標停止執行代碼移動500ms任何幫助,將不勝感激。在此先感謝.. :)

+1

其實你的代碼已經做了這個!?任何時候用戶不會移動鼠標500毫秒會有警報。 _也編輯你的代碼,使它成爲一個正確的語法 - 'onmousemove =(function(){'_ – tenbits

回答

1

我會用這個插件, 這是一個智能高效的代碼來跟蹤mousestop事件。

http://richardscarrott.co.uk/posts/view/jquery-mousestop-event

例如工作示例:http://jsfiddle.net/brunis/wLu4V/

$('html').bind('mousestop', function() { 
    // do timeout here 
}); 
$('html').mousemove(function(event) { 
    // clear timeout here 
}); 

我沒有使用mousestop,你可以,如果你想要的,但它需要一些額外的工作; mousestop只跟蹤每個mouseenter的一次鼠標停留。

如果你想使用mousestop事件的插件,你可以很容易地只使用此代碼:

$.mousestopDelay = 500; // Wait 500 ms before triggering the mousestop event 
$('html').bind('mousestop', function() { 
    alert("Mouse stop + 500 MS!"); 
}); 
+0

500ms部分怎麼樣? –

+0

將這個添加到答案 –