Template:BatteryProperties Example

  • Revision slug: Template:BatteryProperties_Example
  • Revision title: Template:BatteryProperties Example
  • Revision id: 411427
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment If you use this for only one page, please don't make template. Translation will be difficult.

Revision Content

Properties

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

Property Type Description
<%-template("domxref", ["BatteryManager.charging", "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.
<%-template("domxref", ["BatteryManager.chargingTime", "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.
<%-template("domxref", ["BatteryManager.dischargingTime", "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.
<%-template("domxref", ["BatteryManager.level", "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" name="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><%-template("domxref", ["BatteryManager.charging", "charging"])%></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>
  <tr>
    <td><%-template("domxref", ["BatteryManager.chargingTime", "chargingTime"])%></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><%-template("domxref", ["BatteryManager.dischargingTime", "dischargingTime"])%></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><%-template("domxref", ["BatteryManager.level", "level"])%></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 id="Example" name="Example">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