admin管理员组

文章数量:1387447

I have an object that prints the mouse's x and y positions on every mousemove.

It's something like this:

$('#canvas').mousemove(function(e){
    $('#output').prepend(e.pageX + ',' + e.pageY);
});

I've noticed that when you move over the object really fast it only prints out a few positions.

I'm not exactly unhappy that it does that (because it would be quite exhaustive to have it do something for all the hundreds of pixels you've crossed) but I am wondering how this works.

Is the mousemove event limited to a certain amount of triggers per second or what?

(Btw: this was tested on Chromium in Ubuntu Linux)

I have an object that prints the mouse's x and y positions on every mousemove.

It's something like this:

$('#canvas').mousemove(function(e){
    $('#output').prepend(e.pageX + ',' + e.pageY);
});

I've noticed that when you move over the object really fast it only prints out a few positions.

I'm not exactly unhappy that it does that (because it would be quite exhaustive to have it do something for all the hundreds of pixels you've crossed) but I am wondering how this works.

Is the mousemove event limited to a certain amount of triggers per second or what?

(Btw: this was tested on Chromium in Ubuntu Linux)

Share Improve this question asked May 11, 2011 at 18:16 Jelle De LoeckerJelle De Loecker 22k29 gold badges104 silver badges146 bronze badges 1
  • 3 I think it really depends on the browser and puter being used; more memory/faster cpu probably === more iterations per x time. So with that being as it is, I wouldn't heavily depend on mousemove. – Shaz Commented May 11, 2011 at 18:25
Add a ment  | 

3 Answers 3

Reset to default 2

"Mice only report their position to the operating system n times per second, and I think n is usually less than 100"

You may want to look at this, as this may be browser dependent,

http://javascript.info/tutorial/mouse-events#mousemove-and-mouseover-frequency, but, if you look at this question, there is a suggestion on how to get better response.

How to set mousemove update speed?

i think it's synchronous. It's doesn't get triggered for every pixel in which you move your mouse, which means that the events are not queued up .

Say if you have some some code like this.

$('#canvas').mousemove(function(e){
//Some code which takes seconds to execute 
//All subsequent events won't be dispatched while this event handler is executing. 
});

Say if you move mouse while the mouse move event handlers execute. The mousemove handler won't be triggered.

Here is a example for handler which will take seconds to execute. --> http://jsfiddle/78Hf3/1/

And one that take only few time --> http://jsfiddle/78Hf3/2/

本文标签: jqueryHow does the triggering of mousemove work in JavascriptStack Overflow