View Full Version : document.onmousemove question?

12-06-2010, 09:10 PM
Hi I am trying to make a function run if the mouse is moved over the document but when using the object onmousemove it seems to run the code even if the mouse is still over then document, how can I make it so if the mouse is over the document but isn't moving then don't run the code but once the mouse moves run the code?

This is the code I made to handle the mouse move collections.

function onmove(func) {
var oldonmove = document.onmousemove;
if(typeof document.onmousemove != 'function') {
document.onmousemove = func;
} else {
document.onmousemove = function() {

And to use it you would do this.

<script type="text/javascript">
/* Alert A Message Everytime The User Moves Their Mouse */
onmove(function(){ alert('You Moved!'); });

But with this code it runs even when the user doesn't move their mouse and the notification box pops up every second as the code seems to think a still mouse is a moving mouse.

I was thinking about having a run once system but that would mean if the mouse moves it runs once and then if the mouse moves again the code will not run as it has already ran before.

If anyone can help me please reply, Thank You.


12-06-2010, 10:29 PM
The issue that you are seeing is your use of alert in your test code.

onmove(function(){ alert('You Moved!'); });
Whenever alert is called by your javascript the browser loses focus and so when the focus is returned to the browser after the alert is cleared, the browser generates another mousemove event.

If used different code in your test case fore example appending a text node to your document:

var count = 0;
var br = document.createElement('br');
var txt = document.createTextNode("You have moved the mouse " + count++ + " times.");



Or for ease of viewing just replaced the text inside of the body: (Though using innerHTML being bad practice from my understand)

var count = 0;
document.body.innerHTML = "You have moved the mouse " + count++ + " times.";
You probably would have never thought your code was working improperly.

JLBShecky (A novice web programmer obsessed with HTML5)

EDIT: Tested in IE8, IE8 compatibility mode, Chrome 8.0.552.215, FireFox 3.5.15, Safari 5.0.3, and Opera 10.63. I only tested on windows platforms as that is all I have in front of me at the moment, but that should be irrelevant in this case.

12-06-2010, 10:36 PM
Thanks for the reply and I now know what im doing with it but how can I do a thing similar to facebook where if you are on a different tab for a while or inactive for like 1min and then move you mouse anywhere on the document window the live update render newer updates how can I do something like that where it doesn't call the updates when ever the mouse is moved but only if the window was inactive?

12-06-2010, 10:49 PM
I didn't mean to come across as condescending. I consider myself to be a novice web programmer, and for all I know you know more about web programming than I do. I was just attempting to be through, one never knows when some time down the road someone might find a topic to be useful.

That said in the example of retrieving new content if user has been inactive for a certain amount of time I would personally do it so that I keep track of the last time that the user moved the mouse and if the time was greater than a specified delta I would retrieve new content. I would probably use the date object and its getTime function to get the unix time stamp in milliseconds and compare it to a stored value from the last time the user moved the mouse.

12-06-2010, 10:54 PM
lol well I don't mean to sound rude or anything but if you go look at my profile that lists not even half the programming languages I know lol and yes it is nice to give ideas on others topics lol.

Thanks for the idea that's given me the best idea of what I shell do to make it work, then again I may just do a twitter type live update lol.

12-06-2010, 11:03 PM
Just glad to be of some help, hopefully your new best idea will work out.

Personally the only other thing that I would probably do is to integrate the use of setInterval into the code. Probably have some code called by setInterval update the content if the mouse has been move in the last minute.

PS - And no you weren't coming across as being rude, I jsut thought I may have come across as such.:rolleyes: