-
Notifications
You must be signed in to change notification settings - Fork 196
Idle and Idleprovider
Idle
, once watch()
is called, will start a timeout which if expires, will enter a warning state countdown. Once the countdown reaches zero, idle
will broadcast a timeout event indicating the user has timed out (where your app should log them out or whatever you like). If the user performs an action that triggers a watched DOM event that bubbles up to document.html
, this will reset the idle/warning state and start the process over again.
The following methods can be used to configure Idle
:
-
interrupt(events) (
string
, default'mousemove keydown DOMMouseScroll mousewheel mousedown'
): These are the DOM events the service will watch to reset the idle timeout. Multiple events should be separated by a space. Note: Prior to v1.0, this method is calledactiveOn
. -
idle(seconds) (
integer
, default is 20min): The idle timeout duration in seconds. After this amount of time passes without the user performing an action that triggers one of the watched DOM events, the user is considered idle. Note: Prior to v1.0, this method is calledidleDuration
. -
timeout(seconds) (
integer
, default is 30s): The amount of time the user has to respond (in seconds) before they have been considered timed out. Set to0
orfalse
to disable this feature, if you wantIdle
to do nothing but detect when a user is idle or not forever. Note: Prior to v1.0, this method is calledwarningDuration
, and could not be disabled; it always had to have a positive integer value. -
autoResume(string) (default is
idle
): Possible values areoff
/false
,idle
/true
, ornotIdle
. Whentrue
oridle
, user activity will automatically interrupt the warning countdown and reset the idle state. Iffalse
oroff
, you will need to manually callwatch()
when you want to start watching for idleness again. IfnotIdle
, user activity will only automatically interrupt if the user is not yet idle. -
keepalive(enabled) (
boolean
, default istrue
): Whentrue
, theKeepalive
service is automatically stopped and started as needed.
-
running(): returns whether or not the
watch()
has been called and it is watching for idleness. - idling(): returns whether or not the user appears to be idle.
- watch(): starts watching for idleness, or resets the idle/warning state and continues watching.
- unwatch(): stops watching for idleness, and resets the idle/warning state.
-
setIdle(int) (new: v1.0.0): updates the idle value (see
IdleProvider.idle()
above) and restarts the watch if its running. -
setTimeout(int) (new: v1.0.0): updates the timeout value (see
IdleProvider.timeout()
above) and restarts the watch if its running. - getIdle() (new: v1.1.0): gets the current idle value.
- getTimeout() (new: v1.1.0): gets the current timeout value.
- interrupt(): manually trigger the idle interrupt that normally occurs during user activity.
The events Idle
watches are configurable. It is important to note that if the watched user events are prevented from bubbling up, Idle
will not be reset and thus not work properly. Usually this is not a problem.
interrupt()
is similar to watch()
under certain conditions (in fact, it calls watch()
). interrupt()
will reset the user's idle state (effectively making them "not idle") under the following conditions:
- The idle service is currently running.
- The user's session has not already timed out or expired.
-
autoResume
isidle
/true, orautoResume
isnotIdle
and the user is not yet idle.
The purpose of interrupt is to only reset the user's idle state if the user is allowed to return from idle at that point in time. watch()
on the other hand completely ignores the rules and the user's current idle state and makes them active, and starts watching for idleness again. Calling watch()
will always make them active and start watching for idleness. Internally, monitored DOM events call interrupt()
, which effectively means that DOM events (such as clicking or scrolling) can make a user active if they haven't already timed out and your configuration allows autoResume
.
You should only use interrupt()
if you have custom events or logic that you want to interrupt idleness according to the rules above. Otherwise, use watch()
when you always want idle state reset. In almost all cases, you will likely just use watch()
.
By default, Idle
will start Keepalive
when watch()
is called, and stop it when unwatch()
is called. It will also stop Keepalive
when the user goes idle, and start when they return. It will immediately ping once when they return, and then resume on the usual Keepalive
interval after that.