BatteryManager:charging 属性

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

安全上下文: 此项功能仅在一些支持的浏览器安全上下文(HTTPS)中可用。

BatteryManager 接口的 charging 只读属性是一个布尔值,指示设备的电池当前是否正在充电。当其值发生变化时,会触发 chargingchange 事件。

如果电池正在充电或者用户代理无法报告电池状态信息,则该值为 true。否则,它是 false

一个布尔值。

示例

HTML

html
<div id="charging">(充电状态未知)</div>

JavaScript

js
navigator.getBattery().then((battery) => {
  const charging = battery.charging;

  document.querySelector("#charging").textContent = charging;
});

规范

Specification
Battery Status API
# dom-batterymanager-charging

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
charging

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

参见