Is There A Generic Window.onevent In Javascript?
Solution 1:
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);
Solution 2:
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.
Solution 3:
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.
Solution 4:
You can create a CustomEvent and then dispatch it, like my example:
var allEvents = newCustomEvent("all", {
detail: {
time: newDate()
},
bubbles: true,
cancelable: true
});
functiononEvt(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');
});
Solution 5:
There is no generic event for that, but you can write your own function based on Idle Timer by Nicholas C. Zakas.
Post a Comment for "Is There A Generic Window.onevent In Javascript?"