简体   繁体   English

Web应用程序如何检测用户的空闲时间?

[英]How can a web application detect the user's idle time?

I am working on a browser-based instant messaging client, and this application needs to know when the user is idle, if possible. 我正在使用基于浏览器的即时消息客户端,如果可能,此应用程序需要知道用户何时空闲。 Is there a way, in any major browser, for the application to detect the user's idle time system-wide? 在任何主流浏览器中,应用程序是否有办法检测系统范围内用户的空闲时间? Failing that, are there any recommended techniques for detecting when the user was last active in the application itself, and possibly in other pages under the same domain? 如果失败,是否有推荐的技术来检测用户何时最后一次在应用程序本身中以及在同一域下的其他页面中处于活动状态?

If user goes to different page, we can assume he's not idle, so the problem gets reduced to 'is user active on current page?' 如果用户转到其他页面,我们可以假定他没有闲着,那么问题就减少为“用户在当前页面上是否活跃?”

You can set timeout in onMouseMove event of body. 您可以在body的onMouseMove事件中设置超时。 When timeout fires, you know user's been inactive for some period of time. 当超时触发时,您知道用户在一段时间内处于非活动状态。 If it doesn't, and you receive a onMouseMove before timeout, you simply clear the timeout. 如果不是这样,并且您在超时之前收到onMouseMove ,则只需清除超时即可。 You can also deal with onKeyPress in the same way. 您也可以用相同的方式处理onKeyPress

This could be a little harder if user would open many tabs of your page, but I would just use localStorage for some way of communication between those tabs. 如果用户打开页面的许多选项卡,这可能会有点困难,但是我将使用localStorage在这些选项卡之间进行某种通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM