BatteryManager: levelchange event
The levelchange
event of the Battery Status API is fired when the battery level
property is updated.
Syntax
Use the event name in methods like addEventListener()
, or set an event handler property.
addEventListener('levelchange', event => { });
onlevelchange = event => { };
Event type
A generic Event
.
Example
HTML
<div id="level">(battery level unknown)</div>
<div id="stateBattery">(charging state unknown)</div>
JavaScript
navigator.getBattery().then(battery => {
battery.onlevelchange = () => {
document.querySelector('#level').textContent = battery.level;
if(battery.charging) {
document.querySelector('#stateBattery').textContent =
"Charging time: " + (battery.chargingTime / 60);
}
else {
document.querySelector('#stateBattery').textContent =
"Discharging time: " + (battery.dischargingTime / 60);
}
};
});
Specifications
Specification |
---|
Battery Status API # ref-for-dfn-levelchange-1 |
Battery Status API # dom-batterymanager-onlevelchange |
Browser compatibility
BCD tables only load in the browser