jQuery 如果用户处于活动状态;重新加载 div,如果用户不活动则停止重新加载
我有以下场景。 我的 Twitter feed 应每 5 秒刷新一次(通过使用 load() 函数)。尽管我一直在监控我的带宽,但它确实消耗了很多。
我的想法是,一旦用户处于非活动状态(例如,页面上 5 或 10 秒没有交互),就停止刷新。我尝试了一些插件但没有成功。
刷新过程如下:
setInterval(function() { $("#feed").load("feed.php"); }, 5000);
它应该停止刷新,但我尝试的插件只是不断刷新(运行间隔)。
有什么办法可以实现这一点吗?我尝试了以下插件: - http://www.bedroomlan.org/coding/detecting-'idle'-和-'离开'-超时-javascript - http://www.erichynds.com /jquery/a-new-and-improved-jquery-idle-timeout-plugin/
提前致谢。
I have the following scenario.
My twitter feed should refresh every 5 seconds (by using the load() function). Although I have been monitoring my bandwidth and it really sucks up a lot.
My idea was to stop refreshing as soon as the user goes inactive (say, 5 or 10 seconds no interaction on the page). I tried a few plugins but without success.
The refreshing goes as follows:
setInterval(function() { $("#feed").load("feed.php"); }, 5000);
It should stop refresh, but the plugins I tried just kept refreshing (running the interval).
Is there any way to achieve this? I tried the following plugins:
- http://www.bedroomlan.org/coding/detecting-‘idle’-and-‘away’-timeouts-javascript
- http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是使用
idleTimer
jQuery 插件。之一它的功能是获取自用户上次活动以来的时间量。它允许您编写以下代码。
The easiest way is to use the
idleTimer
jQuery plugin.One of it's capabilities is to get the amount of time since the user was last active. It allows you to write the following code.
跟踪鼠标的位置怎么样?
每隔 5 秒或每当检查一次鼠标坐标是否与上次检查时相同。如果它们相同,则假设鼠标没有移动。那就不要刷新你的提要。如果它们不同,请刷新您的 Feed。
只是一个想法。
How about tracking where the mouse is?
Every 5 seconds or whenever, see if the mouse's coordinates are the same as they were in the previous check. If they are the same, then presume the mouse did not move. Don't refresh your feed then. If they are different, refresh your feed.
Just a thought.