Idle API

Non-standard
This feature is not on a current W3C standards track, but it is supported on the Firefox OS platform. Although implementations may change in the future and it is not supported widely across browsers, it is suitable for use in code dedicated to Firefox OS apps.

This API is available on Firefox OS for internal applications only.

The Idle API is used to notify an app when the user is idle. This lets an app take action when the user is doing nothing with his device. The most common use case is to save battery power; in this case, it is commonly used in conjuction with the Power Management API.

Watching for an idle user

When an application wants to be notified when the system is idle, it must register an idle observer. An idle observer is an object that carries three properties:

  • A time property that defines the time, expressed in seconds, that must pass after the user's last action before the user is considered to be idle.
  • An onidle property, which is a function called when the user is considered idle.
  • An onactive property, which is a function called when the user becomes active after being idle.

Example: dimming the screen when the user is idle

In this example, an idle observer is set up that dims the screen's brightness to 50% when the user is idle for 10 seconds, and restores it back to 100% when the user is active again. A second observer is set up that turns off the screen when the user is idle for at least 15 seconds.

JavaScript
// NOTE: mozPower is part of the Power Management API

var fadeLight = {
  time: 10, // Ten seconds

  onidle: function () {
    // The user does not seem active, let's dim the screen down
    navigator.mozPower.screenBrightness = 0.5;
  },

  onactive: function () {
    // Ok, the user is back, let's brighten the screen up
    navigator.mozPower.screenBrightness = 1;
  }
}

var screenOff = {
  time: 15, // fifteen seconds

  onidle: function () {
    // Ok, the user had his chance but he's really idle, let's turn the screen off
    navigator.mozPower.screenEnabled = false;
  },

  onactive: function () {
    // Ok, the user is back, let's turn the screen on
    navigator.mozPower.screenEnabled = true;
  }
}

// Register the idle observers

navigator.addIdleObserver(fadeLight);
navigator.addIdleObserver(screenOff);

This code defines two idle observer objects: fadeLight and screenOff, then calls navigator.addIdleObserver() once for each of them to register them with the system. Applications can set up as many idle observers as needed.

If the application no longer needs to watch for the user to become idle, it can remove idle observers by calling the navigator.removeIdleObserver() method, as seen below:

JavaScript
navigator.removeIdleObserver(fadeLight);
navigator.removeIdleObserver(screenOff);

Specification

Not part of any specification yet; however, this API will be discussed at W3C as part of the System Applications Working Group.

See also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/idle_api

API Firefox OS Idle Non-standard Overview Reference