jQuery:减少触发 mousemove 事件的频率
我正在尝试找出一种简洁的方法来聚合 mousemove 事件,以便确保我的代码被调用,但每 250-300 毫秒只调用一次。
我考虑过使用类似下面的东西,但想知道是否有更好的模式,或者 jQuery 提供的东西可以做同样的事情:
var mousemove_timeout = null;
$('body').mousemove(function() {
if (mousemove_timeout == null) {
mousemove_timeout = window.setTimeout(myFunction, 250);
}
});
function myFunction() {
/*
* Run my code...
*/
mousemove_timeout = null;
}
编辑: 下面接受的答案将非常适合这个但是,我发现答案中提供的 mousestop()
功能实际上消除了我对聚合的需要,因此,如果您正在阅读此问题并寻找答案,请查看 mousestop 插件是您真正需要的!
I'm trying to figure out a clean way to aggregate mousemove events so that I ensure my code gets called, but only once every 250-300 milliseconds.
I've thought about using something like the following, but was wondering if there was a better pattern, or something jQuery provides that will do the same thing:
var mousemove_timeout = null;
$('body').mousemove(function() {
if (mousemove_timeout == null) {
mousemove_timeout = window.setTimeout(myFunction, 250);
}
});
function myFunction() {
/*
* Run my code...
*/
mousemove_timeout = null;
}
EDIT: The accepted answer below would work perfectly for this situation, however, I found that the mousestop()
functionality provided in the answer actually eliminated my need for the aggregation, so if you're reading this question and looking for an answer, see if the mousestop plugin is what you really need!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在我尝试了接受的答案中的解决方案后,我发现如果鼠标不断移动,特别是在圆周运动中,mousemove() 事件会连续触发,但鼠标坐标保持不变。
所以我想出了一个更简单的解决方案,消除了 mousestop() 和 setTimeout。
这将大约每 100 毫秒正确调用一次handleMouseMove()。 (请注意,我说的是大约,因为 JavaScript 中的时间延迟和间隔不能保证实时)
After I tried the solution in the accepted answer, I found out that if the mouse keep moving constantly, especially in circular motion, mousemove() event is fired continuously, but the mouse coordinates remain the same.
So I came up with a simpler solution which eliminates mousestop() and setTimeout.
This will correctly call handleMouseMove() approximately every 100ms. (Note that I said approximately because time delays and intervals in JavaScript is not real-time guaranteed)
您的代码很好,只是您应该清除超时,然后再将其设置为 null 或它可能会泄漏:
作为替代方案,您可以使用 mousemove/mousestop与 window.setInterval 结合使用
Your code is fine except that you should clear the timeout before setting it to null or it might leak:
As an alternative you could use mousemove/mousestop in conjunction with window.setInterval
一个解决方案和一个问题^^
这种没有全局变量的方法怎么样?这是一个合适的解决方案吗?
A solution and a question^^
What about this approach without a global var. Is that a suitable solution?
在自定义的毫秒时间内获取鼠标位置的简单方法
A simple way of gettin the mouse position in a custom period of miliseconds
您寻求代码节流/去抖动。
http://benalman.com/projects/jquery-throttle-debounce-plugin/
http://drupalmotion.com/article/debounce-and-throttle-visual-explanation
来自 underscore.jS 的示例
You seek code Throttling / Debouncing.
http://benalman.com/projects/jquery-throttle-debounce-plugin/
http://drupalmotion.com/article/debounce-and-throttle-visual-explanation
Sample from underscore.jS
我知道我来晚了一点,但这可能对访问此帖子的人有用,这是我的 2 美分。
使用模运算符和简单的数字增量,您可以以最小的性能影响来限制函数的触发率,如下所示;
此外,如果您害怕达到最大整数限制,您可以每 X 千次点击重置触发的变量(再次使用模数运算符)或使用 mouseout 事件。
I know I'm a little late to the party, but it might be of use to people visiting this thread, here is my 2 cents.
Using the modulus operator and simple number increments, you can throttle the firing rate of your function with minimal performance hit, like so;
Additionally, if you're afraid to hit the max integer limit, you can reset the fired variable every X thousand hits (again, using the modulus operator) or by using the mouseout event.
这是一个非常有趣的问题。我找到了一种不太黑客的方法来做到这一点,你可以看看这个现场演示以下代码片段的 :
This was a really interesting question. I found a less hackish way to do this on, and you can check out this live demo of the following snippet:
您可以通过使用超时来清空计时器来节省几行:
You can save a few lines by using the timeout to null the timer:
为什么不使用 setInterval() 代替超时?
Why not use setInterval() instead of timeouts?