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 |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
levelchange event |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.