最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MouseMove event repeating every second - Stack Overflow

programmeradmin5浏览0评论

/

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.

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.

Share Improve this question edited Nov 3, 2013 at 4:25 Scott Mermelstein asked Jul 23, 2013 at 18:44 Scott MermelsteinScott Mermelstein 15.4k4 gold badges49 silver badges77 bronze badges 11
  • 1 I cannot reproduce this... – Naftali Commented Jul 23, 2013 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 Commented Jul 23, 2013 at 18:49
  • 1 did you considered a hardware issue with your pointer device? – chrmod Commented Jul 23, 2013 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 Commented Jul 23, 2013 at 18:50
  • 3 We found the same issue in our WebApp and reported it to the Chrome team. They confirmed the issue: Please vote here to have it fixed. – Sebastian Commented Nov 12, 2015 at 15:58
 |  Show 6 more comments

3 Answers 3

Reset to default 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.)

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.
});

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.

发布评论

评论列表(0)

  1. 暂无评论