Traducción en curso

« XPCOM API Reference

Please add a summary to this article.
  Last changed in Gecko 1.9a


The idle service lets you monitor how long the user has been 'idle', i.e. not used their mouse or keyboard. You can get the idle time directly, but in most cases you will want to register an observer for a predefined interval.

Currently nsIIdleService implementations exist for Windows, Mac OS X, and Linux (via XScreenSaver).

Implemented by:;1. To create an instance, use:

var idleService = Components.classes[";1"]

Method overview

void addIdleObserver(in nsIObserver observer, in unsigned long time)
void removeIdleObserver(in nsIObserver observer, in unsigned long time)


Attribute Type Description
idleTime long The amount of time in milliseconds that has passed since the last user activity.Read only.



Add an observer to be notified when the user idles for some period of time, and when they get back from that.

void addIdleObserver(
  in nsIObserver observer,
  in unsigned long time
the observer to be notified
time the amount of time in seconds the user should be idle before the observer should be notified.
  • The subject of the notification the observer will get is always the nsIIdleService itself. When the user goes idle, the observer topic is 'idle' and when they get back, the observer topic is 'back'. The data param for the notification contains the current user idle time.
  • You can add the same observer twice.
  • Most implementations need to poll the OS for idle info themselves, meaning your notifications could arrive with a delay up to the length of the polling interval in that implementation. Current implementations use a delay of 5 seconds.


Remove an observer registered with addIdleObserver.

void removeIdleObserver(
  in nsIObserver observer,
  in unsigned long time
the observer to be removed
the amount of time they were listening for.

Removing an observer will remove it once, for the idle time you specify. If you have added an observer multiple times, you will need to remove it just as many times.

Example Code

Example 1:

var idleService = Components.classes[";1"]
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.

Example 2:

var idleService = Components.classes[";1"]
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);

Etiquetas y colaboradores del documento

Colaboradores en esta página: mdnwebdocs-bot, SphinxKnight, teoli, Mgjbot
Última actualización por: mdnwebdocs-bot,