nsISupports
Last changed in Gecko 16 (Firefox 16 / Thunderbird 16 / SeaMonkey 2.13)You can get the idle time directly, but in most cases you will want to register an observer for a predefined interval. The observer will get an 'idle' notification when the user is idle for that interval (or longer), and receive a 'back' (Gecko 3 to 15) or 'active' (Gecko 16+) notification when the user starts using their computer again.
Currently nsIIdleService
implementations exist for Windows, Mac OS X, and Linux (via XScreenSaver).
Implemented by: @mozilla.org/widget/idleservice;1
. To create an instance, use:
var idleService = Components.classes["@mozilla.org/widget/idleservice;1"] .getService(Components.interfaces.nsIIdleService);
void addIdleObserver(in nsIObserver observer, in unsigned long time); |
void removeIdleObserver(in nsIObserver observer, in unsigned long time); |
Attribute | Type | Description |
idleTime |
unsigned long |
The amount of time in milliseconds that has passed since the last user activity. Can be 0 if there is no valid idle time to report (this can happen if the user never interacted with the browser at all, and if we are unable to poll for idle time manually). Read only. |
Add an observer to be notified when the user idles for some period of time, and when they get back from that.
nsIIdleService
itself. When the user goes idle, the observer topic is 'idle' and when they get back, the observer topic is 'back' in Gecko 15 and earlier or 'active' in Gecko 16 and later. The data parameter for the notification contains the current user idle time in Gecko 15 and earlier or 0 in Gecko 16 and later.void addIdleObserver( in nsIObserver observer, in unsigned long time );
observer
nsIObserver
to be notified.time
Remove an observer registered with addIdleObserver()
.
void removeIdleObserver( in nsIObserver observer, in unsigned long time );
observer
nsIObserver
to be removed.time
var idleService = Components.classes["@mozilla.org/widget/idleservice;1"] .getService(Components.interfaces.nsIIdleService) setTimeout(function() { alert(idleService.idleTime) }, 1000) // if you don't use the mouse or the keyboard after running this snippet, // you'll see a number around 1000 alerted.
var idleService = Components.classes["@mozilla.org/widget/idleservice;1"] .getService(Components.interfaces.nsIIdleService) var idleObserver = { observe: function(subject, topic, data) { alert("topic: " + topic + "\ndata: " + data); } }; idleService.addIdleObserver(idleObserver, 60); // one minute // ... // Don't forget to remove the observer using removeIdleObserver! idleService.removeIdleObserver(idleObserver, 60);