10

http://jsfiddle.net/MrkY9/

My computer (and so far, no other computer among my coworkers) is exhibiting an issue in Chrome, IE, and Safari (but not in Firefox). Simple mousemove code, such as the following (already running on the fiddle above) properly catches mousemove events, but then as long as the mouse is in the div, catches a mousemove event every second - even though I'm no longer moving the mouse.

var number = 0;
$("#foo").on("mousemove", function() { this.innerHTML = number++ });

This seems to be a browser-based problem, since it doesn't exhibit on FireFox. (Nor does it occur on Windows itself. Even when the counter is going up, if I leave my keyboard and mouse alone, my screen saver eventually kicks in.) Before concluding it's not a system issue, I tried replacing the mouse and switching the USB port it's plugged into. Not surprisingly, none of those solutions resolve the issue.

I haven't figured out how to test this in anything other than javascript in a browser.

Questions: Has anyone encountered this before? Is there anything I need to do to catch it? I have code far less trivial than this fiddle that rely on knowing when the mouse is and isn't moving.

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • 1
    I cannot reproduce this... – Naftali Jul 23 '13 at 18:46
  • 3
    It might be possible to work around the issue, by saving the mouse position, and checking whether the position is indeed different before doing whatever needs to be done. – sabof Jul 23 '13 at 18:49
  • 1
    did you considered a hardware issue with your pointer device? – chrmod Jul 23 '13 at 18:49
  • 2
    By chance are you using some kind of keep-alive program that defeats the screen saver? Or something else that my be emulating a mouse movement every second? Try eliminating background programs one at a time to see if you can resolve the issue. – DevlshOne Jul 23 '13 at 18:50
  • @sabof Thanks, that what I've been trying, but we actually have use cases where it's possible that we want the event to fire when the position is the same (e.g. two different containers on a touchscreen, clicking one then the other then the first in the exact same position - in that case, we'd skip the mousemove event of the click.) – Scott Mermelstein Jul 23 '13 at 18:51
  • @chrmod I've tried 3 different mouse, in all different ports. And the screensaver works, so it's not a system-level issue. And FireFox works (or is smart enough to catch the continuous events). – Scott Mermelstein Jul 23 '13 at 18:52
  • @DevlshOne I haven't deliberately run any kind of keep-alive, and in fact the screen saver does kick in. Between that and the fact that it works properly in FireFox, I'm thinking it's a browser issue. – Scott Mermelstein Jul 23 '13 at 18:53
  • 1
    Maybe a plugin in Firefox is making it un-broken since every other browser seems to be affected. – DevlshOne Jul 23 '13 at 18:56
  • @DevlshOne I like that idea, but my plugins are all very standard, and the code still works in FireFox when I disable my two developer extensions (firebug and gwt) – Scott Mermelstein Jul 23 '13 at 19:00
  • 2
    We found the same issue in our WebApp and reported it to the Chrome team. They confirmed the issue: Please vote [here](https://code.google.com/p/chromium/issues/detail?id=547030) to have it fixed. – Sebastian Nov 12 '15 at 15:58
  • 1
    As of January 30, 2017 this problem is marked as fixed in Chrome. I haven't checked IE, edge, or safari to see if they fixed the issue, though. – Scott Mermelstein Mar 04 '17 at 19:56

3 Answers3

19

Ok, I found the problem, though I don't fully understand why it was an issue.

I had Task Manager running in the background. And for some reason, every time it updated itself, it was causing IE, Safari and Chrome to receive a mousemove event.

It doesn't make sense, but at least the fix is simple: close the Task Manager.

(It's very obvious if you are in the Applications tab. If you're in Performance, it depends on what the values are set to.)

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • 2
    Had the same problem. For me it was because I had Flux running and it was currently changing the monitor's screen colors. – EricP Oct 14 '13 at 01:38
  • 4
    wtf, this is exactly what happens if you have an open taskmanager - thanks, you saved me a lot of time (and increasing grey hairs)! – KIC Nov 02 '13 at 15:59
  • 3
    @KIC Glad my lot of time and grey hairs could save you a few. :-) – Scott Mermelstein Nov 03 '13 at 04:24
  • Had the same problem; closed TM and now it works. Thank you for saving my precious hair! – Eric Jul 10 '14 at 09:41
  • 2
    I have the same issue on Win Server 2008 / Chrome 36 when either Server Manager or Fiddler are open, Chrome but not Firefox. – johnhunter Jul 22 '14 at 13:22
  • Had the same problem and Google brought me to this link which the fixed worked. Can confirm still is sending ~1 mousemove every sec with Task Manager open in Chrome. – WacławJasper Jan 06 '15 at 05:26
  • Wow! Incredible answer! I checked all my js scripts while looking for an error, could not think that task manager affects IE mousemove event. – Sergey Zhukov May 25 '15 at 00:42
  • 1
    For me, the cause was HeidiSQL. They have a paypal donate button which changed text every 10 seconds and that triggers 2 mousemoves. – Miro Feb 04 '16 at 14:19
  • Thanks a lot, I was having the same issue and not knowing why until I saw your answer here. I closed the task manager and the mouse move event stopped firing when the cursor was still. – schrodinger's code Jul 12 '16 at 21:03
  • Sad that this is still an issue 2 years later. If you look at the link pointed to by Sebastian in the comment on the question, at least as of June 21, 2016, it's assigned to someone at chromium.org to fix, but not yet fixed. – Scott Mermelstein Jul 12 '16 at 21:12
  • 1
    Glad they finally fixed this in the newest builds. I thought I was going crazy for a minute there. – Ajedi32 Mar 04 '17 at 19:10
  • At least it works right in Chrome now. Thanks for letting us know! – Scott Mermelstein Mar 04 '17 at 19:54
4

For me the issue happens sometimes when iTunes is playing. I know iTunes (for Windows) has had a focus issue for ages - it tends to steal focus from its own popup windows.

You pointed out the problem in your (accepted) answer: other applications can steal the browser's focus, firing the mousemove event at will. However for a live website we cannot assume a user is not running certain programs like Task Manager or iTunes.

As suggested in the question's comment section, please save the mouse position and keep checking if it changed.

Example code using jQuery:

var old_pos = [0, 0];
$(el).on("mousemove", function(e) {
    var new_pos = [e.clientX, e.clientY];

    // Quit if the mouse position did not change.
    if(old_pos[0] == new_pos[0] && old_pos[1] == new_pos[1])
        return;

    // Your code.
});

(Note: you're better off using the touchmove event with the touches and changedTouches arrays for touch gestures.)

Hope this helps anyone bumping into similar problems.

Edit: additional functionality to separate mouse events from touch events based on Scott's comment below:

var old_pos = [0, 0];
$(el).on("mousemove touchmove", function(e) {
    var new_pos = [e.clientX, e.clientY];

    // Quit if the mouse position did not change.
    if(e.type == "mousemove" && old_pos[0] == new_pos[0] && old_pos[1] == new_pos[1])
        return;

    // Your code.
});
Robbert
  • 5,063
  • 6
  • 36
  • 44
  • Thanks for your additional info. Just to clarify the circumstance I had - we wanted to only make one part of our code deal with mouse/touch events, so had something catching the touch events and forwarding them on as a mouse event. Since a simulated mouse click fires a move and a mousedown event, we had to allow move events with 0 movement. – Scott Mermelstein Aug 12 '13 at 22:02
  • Makes sense. I probably would've tried working around it, for example by listening for both `mousemove` and `touchmove` and separating them via `e.type` inside the event listener (see my edited post above). Since both `mousedown` and `mousemove` fire at the same time another solution could be to add an event listener for `mousedown`, record the start coordinates there and disregard the initial `mousemove`. Anyway, you've already solved the issue, so these are just some ideas for next time you run into mouse event trouble. – Robbert Aug 14 '13 at 22:56
  • Thanks for the addition. There are definitely ways to work around the issue (though that would have required changing code in many places just to handle an extremely edge case), but the bigger deal for us was finding the root cause. As long as we know the root cause, we're perfectly happy telling the customer to stop using task manager - or iTunes. :-) – Scott Mermelstein Aug 15 '13 at 03:42
0

I faced the exact same issue. For Pure Javascript, I replaced onmousemove with onmouseover. For jQuery, I replaced mousemove with mouseover. Fastest fix for me.

SILENT
  • 3,916
  • 3
  • 38
  • 57