开发者

Is there a generic window.onevent in javascript?

开发者 https://www.devze.com 2023-02-03 21:19 出处:网络
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 i开发者_JAVA百科n JavaScript there are specific windo

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 i开发者_JAVA百科n 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?


There's no inactive event that I'm aware of.

I'm assuming JQ is fair game since you tagged it. You can bind multiple events with one bind statement so something like this should work:

$('body').bind('mousedown keydown mousemove', resetInactiveTimer);


Unfortunately there is no generic window.onevent in javascript.

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.

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.

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.


You would not want to capture all events, as e.g. loading and mutation events are unrelated to user activity. I am unaware of a browser interface for capturing many events at once.

I would register multiple event handlers for multiple purposes. When a key is down, or the mouse button is down, obviously the user is not idle no matter how long they hold it. I would begin a timer after the last release, reset it on mousemove or scroll, and clear it on first press.


You can create a CustomEvent and then dispatch it, like my example:

var allEvents = new CustomEvent("all", {
    detail: {
        time: new Date()
    },
    bubbles: true,
    cancelable: true
});

function onEvt(evt) {
    switch (evt.type) {
        case 'abort': case 'beforeunload': case 'blur': case 'change': case 'click': case 'close': case 'contextmenu': case 'devicelight':
        case 'devicemotion': case 'deviceorientation': case 'deviceproximity': case 'dragdrop': case 'error': case 'focus': case 'hashchange':
        case 'keydown': case 'keypress': case 'keyup': case 'load': case 'mousedown': case 'mousemove': case 'mouseout': case 'mouseover':
        case 'mouseup': case 'mozbeforepaint': case 'paint': case 'popstate': case 'reset': case 'resize': case 'scroll': case 'select':
        case 'submit': case 'unload': case 'userproximity': case 'pageshow': case 'pagehide':
            evt.currentTarget.dispatchEvent(allEvents);
            break;
    }
}

window.addEventListener('click', onEvt);

window.addEventListener('all', function(evt) {
    alert('mwahahahahaha');
});


There is no generic event for that, but you can write your own function based on Idle Timer by Nicholas C. Zakas.

0

精彩评论

暂无评论...
验证码 换一张
取 消