mozilla

Revision 92908 of nsIIdleService

  • Revision slug: XPCOM_Interface_Reference/nsIIdleService
  • Revision title: nsIIdleService
  • Revision id: 92908
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment 31 words added, 4 words removed; page display name changed to 'nsIIdleService'

Revision Content

{{ Xpcomapi() }} {{ Fx_minversion_header("3") }}

{{ InterfaceStatus("nsIIdleService", "widget/public/nsIIdleService.idl", "unfrozen", "Mozilla 1.9a", "yes") }}

Summary

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: @mozilla.org/widget/idleservice;1. To create an instance, use:

var idleService = Components.classes["@mozilla.org/widget/idleservice;1"]
                            .getService(Components.interfaces.nsIIdleService)

Method overview

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

Attributes

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

Methods

addIdleObserver()

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
)
Parameters
observer
The observer to be notified.
time
The amount of time in seconds the user should be idle before the observer should be notified.
Remarks
  • 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'. In addition, there is a once a day notification sent out if the user remains idle for an extended period: 'idle-daily'. The data parameter 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.

removeIdleObserver()

Remove an observer registered with addIdleObserver.

void removeIdleObserver(
  in nsIObserver observer,
  in unsigned long time
)
Parameters
<tt>observer</tt>
the observer to be removed
<tt>time</tt>
the amount of time they were listening for.
Remarks

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["@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.

Example 2:

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);

Interwiki Language Links

{{ languages( { "ja": "ja/NsIIdleService", "pt": "pt/NsIIdleService" } ) }}

Revision Source

<p>
{{ Xpcomapi() }}
{{ Fx_minversion_header("3") }}
</p><p>{{ InterfaceStatus("nsIIdleService", "widget/public/nsIIdleService.idl", "unfrozen", "Mozilla 1.9a", "yes") }}
</p>
<h2 name="Summary">Summary</h2>
<p>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.
</p><p>Currently <code>nsIIdleService</code> implementations exist for Windows, Mac OS X, and Linux (via XScreenSaver).
</p><p>Implemented by: <code>@mozilla.org/widget/idleservice;1</code>. To create an instance, use:
</p>
<pre class="eval">var idleService = Components.classes["@mozilla.org/widget/idleservice;1"]
                            .getService(Components.interfaces.nsIIdleService)
</pre>
<h2 name="Method_overview">Method overview</h2>
<table class="standard-table"> <tbody><tr>
<td> <code>void <a href="#addIdleObserver.28.29">addIdleObserver</a>(in <a href="en/NsIObserver">nsIObserver</a> observer, in unsigned long time)</code>
</td></tr>
<tr>
<td> <code>void <a href="#removeIdleObserver.28.29">removeIdleObserver</a>(in <a href="en/NsIObserver">nsIObserver</a> observer, in unsigned long time)</code>
</td></tr>
</tbody></table>
<h2 name="Attributes">Attributes</h2>
<table class="standard-table"> <tbody><tr>
<td class="header">Attribute
</td><td class="header">Type
</td><td class="header">Description
</td></tr>
<tr>
<td><code>idleTime</code>
</td><td><code>long</code>
</td><td>The amount of time in milliseconds that has passed since the last user activity. <i>Read only.</i>
</td></tr>
</tbody></table>
<h2 name="Methods">Methods</h2>
<h3 name="addIdleObserver.28.29">addIdleObserver()</h3>
<p>Add an observer to be notified when the user idles for some period of time, and when they get back from that.</p>
<pre class="eval">void addIdleObserver(
  in nsIObserver observer,
  in unsigned long time
)
</pre>
<h6 name="Parameters">Parameters</h6>
<dl><dt><code>observer</code> </dt><dd>The <a href="/en/nsIObserver" title="en/nsIObserver">observer</a> to be notified. </dd></dl> <dl><dt><code>time</code> </dt><dd>The amount of time in seconds the user should be idle before the observer should be notified. </dd></dl>
<h6 name="Remarks">Remarks</h6>
<ul> <li>The subject of the notification the observer will get is always the <code>nsIIdleService</code> itself. When the user goes idle, the observer topic is 'idle' and when they get back, the observer topic is 'back'. In addition, there is a once a day notification sent out if the user remains idle for an extended period: 'idle-daily'. The data parameter for the notification contains the current user idle time.</li>
</ul>
<ul> <li>You can add the same observer twice.</li>
</ul>
<ul> <li>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.</li>
</ul><h3 name="removeIdleObserver.28.29"> removeIdleObserver()</h3>
<p>Remove an observer registered with addIdleObserver.
</p>
<pre class="eval">void removeIdleObserver(
  in nsIObserver observer,
  in unsigned long time
)
</pre>
<h6 name="Parameters_2">Parameters</h6>
<dl><dt><tt>observer</tt>
</dt><dd>the <a href="en/NsIObserver">observer</a> to be removed
</dd></dl>
<dl><dt><tt>time</tt>
</dt><dd>the amount of time they were listening for.
</dd></dl>
<h6 name="Remarks_2">Remarks</h6>
<p>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.
</p>
<h2 name="Example_Code">Example Code</h2>
<p>Example 1:
</p>
<pre class="eval">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.
</pre>
<p>Example 2:
</p>
<pre class="eval">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);
</pre>
<p><span class="comment">Interwiki Language Links</span>
</p>{{ languages( { "ja": "ja/NsIIdleService", "pt": "pt/NsIIdleService" } ) }}
Revert to this revision