onmousemove continually fires, indefinitely

by timvasil 11/12/2007 12:14:00 AM

I recently decided to hook document.onmousemove and document.onkeypress for idle detection on a web page.  Kind of the right idea, but surprising results. 

Did you know that, at least with IE 7, the onmousemove event continues to fire a couple times per second even when the mouse is not moving?  and even when the browser window doesn't have the focus, so long as the mouse is over the document?  Bizarre. 

There's an easy enough way around this issue:

document.onmousemove = observeMoveActivity;
var g_iOldX;
var g_iOldY;
function observeMoveActivity()
{
    if (event && (event.screenX != g_iOldX || event.screenY != g_iOldY))
    {
        g_iOldX = event.screenX;
        g_iOldY = event.screenY;
        observeActivity();  // do actual work here
    }
}

Tags:

JavaScript

Comments are closed

Search

Calendar

«  February 2012  »
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

View posts in large calendar

Recent comments

Archive