javascript中是否有通用的window.onevent? [英] Is there a generic window.onevent in javascript?

查看:98
本文介绍了javascript中是否有通用的window.onevent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个JavaScript(jQuery也很好)函数来检查用户在当前页面上处于非活动状态的时间。我知道在JavaScript中有特定的窗口事件,如 window.onload window.onmousemove 等。但我是想知道是否有任何功能捕获任何事件(例如鼠标移动,按键或其他任何指示用户在页面上的活动),而不必手动设置所有这些事件监听器?

I am trying to make a JavaScript (jQuery is fine as well) function to check how long a user has been inactive for on the current page. I know that in JavaScript there are specific window events like window.onload, window.onmousemove, etc. But I was wondering if there was any function that captures any event (such as a mouse move, key press, or anything else that indicates activity by the user on the page) without having to set all of those event listeners manually?

推荐答案

不幸的是,javascript中没有通用的window.onevent。

Unfortunately there is no generic window.onevent in javascript.

我做过一个heartbeat脚本就像你描述的那样,当用户在页面上处于活动状态并且要监听的主要事件是 mousemove ,<$时,确保会话保持活动状态c $ c> keydown / keyup 滚动,可能还有窗口焦点。

I've done a "heartbeat" script like you're describing to make sure a session is kept alive while a user is active on a page and the main events to listen to are mousemove, keydown/keyup, scroll and possibly on window focus.

请记住,如果您使用像TinyMCE这样的富文本编辑器,它在页面上运行自己的iFrame,那些事件可能不会被冒泡,您需要进行广泛的测试那些情景。

Keep in mind if you're using a rich text editor like TinyMCE which runs in its own iFrame on the page those events may not get bubbled up and you will want to extensively test those scenarios.

使用jQuery或Mootools等Javascript框架可以更轻松地将单个函数绑定到多个事件或触发自己的事件来触发或重置超时。

Using a Javascript framework like jQuery or Mootools will make it much easier to bind a single function to multiple events or to fire your own events to trigger or reset timeouts.

这篇关于javascript中是否有通用的window.onevent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆