2010-07-16 36 views
3

對於某些功能,我正在處理mousemove事件。 mousemove事件偵聽器通過一個不需要的單個線性鼠標手勢被調用多次。我需要實現一個自定義事件,當鼠標停止運動時它將被調用。我有一個猜測,它可以在mousemove上實現一些延遲功能。Java腳本:mousestop event

請在這方面幫助我。由於

回答

3

你最那裏的方式:

function waitForMouseStop(callback) { 
    var timer; 

    function stoppedMoving(evt) { 
     document.onmousemove = null; 
     callback(); 
    } 

    function moveHandler(evt) { 
     evt = evt || window.event; 
     if (timer) { 
      window.clearTimeout(timer); 
     } 
     timer = window.setTimeout(function() { 
      stoppedMoving(evt); 
     }, 500); 
    } 

    document.onmousemove = moveHandler; 
} 

waitForMouseStop(function() { 
    alert("Stopped"); 
}); 
+0

非常有用的。但我怎樣才能將evt參數傳遞給stoppedMoving? – 2010-07-19 05:01:49

+0

已更新的答案將事件對象傳遞給'stoppedMoving'。 – 2010-07-19 08:19:58