Template:BatteryProperties Example

  • Revision slug: Template:BatteryProperties_Example
  • Revision title: Template:BatteryProperties Example
  • Revision id: 301891
  • Created:
  • Creator: louisremi
  • Is current revision? No
  • Comment

Revision Content

Properties

The event callback doesn't receive any event objects but properties can be read from the navigator.battery interface.

<t>r </t>
Property Type Description
charging boolean The system's battery charging status. Returns true if the battery is charging, if the state of the system's battery is not determinable, or if no battery is attached to the system. Returns false if the battery is discharging. Read only.
chargingTime double (float) The remaining time in seconds until the system's battery is fully charged. Returns 0 if the battery is fully charged. Returns positive infinity, if the battery is discharging or if the implementation is unable to report the remaining charging time. Read only.
dischargingTime double (float) the remaining time in seconds until the system's battery is completely discharged and the system is about to be suspended. Returns positive infinity if the battery is charging, if the implementation is unable to report the remaining discharging time, or if there is no battery attached to the system. Read only.
level double (float) The system's battery charging level scaled from 0 to 1.0. Returns 0 if the battery is empty and the system is about to suspend. Returns 1.0 if the battery is full, if the implementation is unable to report the battery's level, or if there is no battery attached to the system. Read only.

Example

var battery = navigator.battery || navigator.mozBattery || navigator.webkitBattery;

function updateBatteryStatus() {
  alert("Battery status: " + battery.level * 100 + " %");

  if (battery.charging) {
    alert("Battery is charging"); 
  }
}

battery.addEventListener("chargingchange", updateBatteryStatus);
battery.addEventListener("levelchange", updateBatteryStatus);
updateBatteryStatus();

Revision Source

<h2 id="Properties">Properties</h2>

<p>The event callback doesn't receive any event objects but properties can be read from the <code>navigator.battery</code> interface.</p>

<table class="standard-table"><tbody>
  <tr>
    <td class="header" width="150">Property</td>
    <td class="header" width="100">Type</td>
    <td class="header">Description</td>
  </tr>
  <tr>
    <td><code>charging</code></td>
    <td>boolean</td>
    <td>The system's battery charging status. Returns <code>true</code> if the battery is charging, if the state of the system's battery is not determinable, or if no battery is attached to the system. Returns <code>false</code> if the battery is discharging. Read only.</td>
  </tr>
  <t>r
    <td><code>chargingTime</code></td>
    <td>double (float)</td>
    <td>The remaining time in seconds until the system's battery is fully charged. Returns 0 if the battery is fully charged. Returns positive infinity, if the battery is discharging or if the implementation is unable to report the remaining charging time. Read only.</td>
  </tr>
  <tr>
    <td><code>dischargingTime</code></td>
    <td>double (float)</td>
    <td>the remaining time in seconds until the system's battery is completely discharged and the system is about to be suspended. Returns positive infinity if the battery is charging, if the implementation is unable to report the remaining discharging time, or if there is no battery attached to the system. Read only.</td>
  </tr>
  <tr>
    <td><code>level</code></td>
    <td>double (float)</td>
    <td>The system's battery charging level scaled from 0 to 1.0. Returns 0 if the battery is empty and the system is about to suspend. Returns 1.0 if the battery is full, if the implementation is unable to report the battery's level, or if there is no battery attached to the system. Read only.</td>
  </tr>
</table>

<h2>Example</h2>

<pre class="brush: js">
var battery = navigator.battery || navigator.mozBattery || navigator.webkitBattery;

function updateBatteryStatus() {
  alert("Battery status: " + battery.level * 100 + " %");

  if (battery.charging) {
    alert("Battery is charging"); 
  }
}

battery.addEventListener("chargingchange", updateBatteryStatus);
battery.addEventListener("levelchange", updateBatteryStatus);
updateBatteryStatus();
</pre>
Revert to this revision