JavaScript 中 mousemove 的触发是如何工作的?
我有一个对象,可以在每次鼠标移动时打印鼠标的 x 和 y 位置。
事情是这样的:
$('#canvas').mousemove(function(e){
$('#output').prepend(e.pageX + ',' + e.pageY);
});
我注意到,当你非常快地在对象上移动时,它只会打印出几个位置。
我并不完全不高兴它这样做(因为让它为你跨越的所有数百个像素做一些事情会非常详尽),但我想知道这是如何工作的。
mousemove 事件是否限制为每秒一定数量的触发次数?
(顺便说一句:这是在 Ubuntu Linux 中的 Chromium 上测试的)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想看看这个,因为这可能取决于浏览器,
http ://javascript.info/tutorial/mouse-events#mousemove-and-mouseover-Frequency,但是,如果您查看此问题,就会发现有关如何获得更好响应的建议。
如何设置鼠标移动更新速度?
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?
我认为这是同步的。它不会为您移动鼠标的每个像素触发,这意味着事件不会排队。
假设你有一些像这样的代码。
假设您在鼠标移动事件处理程序执行时移动鼠标。
mousemove
处理程序不会被触发。这是处理程序的示例,执行需要几秒钟。 --> http://jsfiddle.net/78Hf3/1/
而且只需要很少的时间 --> ; http://jsfiddle.net/78Hf3/2/
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.
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.net/78Hf3/1/
And one that take only few time --> http://jsfiddle.net/78Hf3/2/
“鼠标每秒仅向操作系统报告其位置 n 次,我认为 n 通常小于 100"
"Mice only report their position to the operating system n times per second, and I think n is usually less than 100"