BatteryManager:levelchange 事件
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
BatteryManager
接口的 levelchange
事件在电池的 level
属性更新时触发。
语法
在 addEventListener()
等方法中使用事件名称,或设置事件处理器属性。
js
addEventListener("levelchange", (event) => { })
onlevelchange = (event) => { }
事件类型
一个通用的 Event
。
示例
HTML
html
<div id="level">(电池电量未知)</div>
<div id="stateBattery">(充电状态未知)</div>
JavaScript
js
navigator.getBattery().then((battery) => {
battery.onlevelchange = () => {
document.querySelector("#level").textContent = battery.level;
if (battery.charging) {
document.querySelector("#stateBattery").textContent = `充电时间:${
battery.chargingTime / 60
}`;
} else {
document.querySelector("#stateBattery").textContent =
`放电时间:${battery.dischargingTime / 60}`;
}
};
});
规范
Specification |
---|
Battery Status API # ref-for-dfn-levelchange-1 |
Battery Status API # dom-batterymanager-onlevelchange |
浏览器兼容性
BCD tables only load in the browser