nsIIdleService 编辑

widget/nsIIdleService.idlScriptable The idle service lets you monitor how long the user has been 'idle', that is they have not used their mouse or keyboard. 1.0 66 Introduced Gecko 1.9.1 Inherits from: 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. 

Note: The idle service is for computer-wide idle detection, not just application idle detection. In other words, even if the user is working in other applications, the idle service will still consider the user to be active.

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

AttributeTypeDescription
idleTimeunsigned longThe 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.

Methods

addIdleObserver()

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

  • 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 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.
  • 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.
Gecko 1.9.2 note Starting in Gecko 1.9.2, 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.
void addIdleObserver(
  in nsIObserver observer,
  in unsigned long time
);
Parameters
observer
The nsIObserver to be notified.
time
The amount of time in seconds the user should be idle before the observer should be notified.

removeIdleObserver()

Remove an observer registered with addIdleObserver().

Note: 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.
void removeIdleObserver(
  in nsIObserver observer,
  in unsigned long time
);
Parameters
observer
The nsIObserver to be removed.
time
The amount of time they were listening for.

Example

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:108 次

字数:6675

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文