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.
Interfaces
BatteryManager
Provides information about the system's battery charge level.
Extensions to other interfaces
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.
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
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on IOS
Samsung Internet
WebView Android
Battery_Status_API
38
79
43–52
25
No
38
43–52
25
No
3.0
38
secure_context_required
103
103
No
No
No
103
No
71
No
20.0
103
Desktop
Mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on IOS
Samsung Internet
WebView Android
Battery_Status_API
38
79
43–52
25
No
38
43–52
25
No
3.0
38
charging
38
79
43–52
25
No
38
43–52
25
No
3.0
38
chargingTime
38
79
43–52
25
No
4238–42 Always equal to 0
or Infinity
.
43–52
2925–29 Always equal to 0
or Infinity
.
No
4.03.0–4.0 Always equal to 0
or Infinity
.
4238–42 Always equal to 0
or Infinity
.
chargingchange_event
38
79
43–52
25
No
38
43–52
25
No
3.0
38
chargingtimechange_event
38
79
43–52
25
No
38
43–52
25
No
3.0
38
dischargingTime
38
79
43–52
25
No
4238–42 Always equal to Infinity
.
43–52
2925–29 Always equal to Infinity
.
No
4.03.0–4.0 Always equal to Infinity
.
4238–42 Always equal to Infinity
.
dischargingtimechange_event
38
79
43–52
25
No
38
43–52
25
No
3.0
38
level
38
79
43–52
25
No
38
43–52
25
No
3.0
38
levelchange_event
38
79
43–52
25
No
38
43–52
25
No
3.0
38
secure_context_required
103
103
No
No
No
103
No
71
No
20.0
103
api.BatteryManager
BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.
api.Navigator.getBattery
BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.
See also