W3cubDocs

/Web APIs

Battery Status API

The Battery Status API, more often referred to as the Battery API, provides information about the system's battery charge level and lets you be notified by events that are sent when the battery level or charging status change. This can be used to adjust your app's resource usage to reduce battery drain when the battery is low, or to save changes before the battery runs out in order to prevent data loss.

Note: This API is not available in Web Workers (not exposed via WorkerNavigator).

Interfaces

BatteryManager

Provides information about the system's battery charge level.

navigator.getBattery() Read only

Returns a Promise that resolves with a BatteryManager object.

Example

In this example, we watch for changes both to the charging status (whether or not we're plugged in and charging) and for changes to the battery level and timing. This is done by listening for the chargingchange, levelchange, chargingtimechange, dischargingtimechange events.

js

navigator.getBattery().then((battery) => {
  function updateAllBatteryInfo() {
    updateChargeInfo();
    updateLevelInfo();
    updateChargingInfo();
    updateDischargingInfo();
  }
  updateAllBatteryInfo();

  battery.addEventListener("chargingchange", () => {
    updateChargeInfo();
  });
  function updateChargeInfo() {
    console.log(`Battery charging? ${battery.charging ? "Yes" : "No"}`);
  }

  battery.addEventListener("levelchange", () => {
    updateLevelInfo();
  });
  function updateLevelInfo() {
    console.log(`Battery level: ${battery.level * 100}%`);
  }

  battery.addEventListener("chargingtimechange", () => {
    updateChargingInfo();
  });
  function updateChargingInfo() {
    console.log(`Battery charging time: ${battery.chargingTime} seconds`);
  }

  battery.addEventListener("dischargingtimechange", () => {
    updateDischargingInfo();
  });
  function updateDischargingInfo() {
    console.log(`Battery discharging time: ${battery.dischargingTime} seconds`);
  }
});

See also the example in the specification.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
Battery_Status_API 38 79 43–52 No 25 No 38 38 43–52 25 No 3.0
charging 38 79 43–52 No 25 No 38 38 43–52 25 No 3.0
chargingTime 38 79 43–52 No 25 No 42
38–42Always equal to 0 or Infinity.
42
38–42Always equal to 0 or Infinity.
43–52 29
25–29Always equal to 0 or Infinity.
No 4.0
3.0–4.0Always equal to 0 or Infinity.
chargingchange_event 38 79 43–52 No 25 No 38 38 43–52 25 No 3.0
chargingtimechange_event 38 79 43–52 No 25 No 38 38 43–52 25 No 3.0
dischargingTime 38 79 43–52 No 25 No 42
38–42Always equal to Infinity.
42
38–42Always equal to Infinity.
43–52 29
25–29Always equal to Infinity.
No 4.0
3.0–4.0Always equal to Infinity.
dischargingtimechange_event 38 79 43–52 No 25 No 38 38 43–52 25 No 3.0
level 38 79 43–52 No 25 No 38 38 43–52 25 No 3.0
levelchange_event 38 79 43–52 No 25 No 38 38 43–52 25 No 3.0
secure_context_required 103 103 No No No No 103 103 No 71 No 20.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API