Navigator.battery

已废弃
该特性已经从 Web 标准中删除,虽然一些浏览器目前仍然支持它,但也许会在未来的某个时间停止支持,请尽量不要使用该特性。

电池状态API,通常简称为电池API,该API能够让开发者访问用户系统的电池电量以及是否外接电源等电源状态信息,并且在电源状态发生变化时引发事件来开发者。开发者就可以在得知系统电量不足的时候降低你的网站上一些循环执行任务的频率,从而节约电量。或者在电量减少到某个级别的时候,自动保存页面上的一些数据,以防止用户的数据丢失。

电池状态API曾经暴露在window.navigatorbattery属性上,但是现在battery属性已经被移除,请使用标准方法Navigator.getBattery()来代替,该方法返回一个包裹电池状态对象的Promise

Syntax

var battery = navigator.battery;

属性

charging
只读. 一个布尔值,表示了系统电池的充电状态.如果电池正在充电,则返回true,其他情况,比如无法获取系统电池的充电状态,或者系统不使用电池,或者电池不在充电,都返回false.
chargingTime
只读. 一个整字,单位为秒.表示了电池还剩多长时间充满电.如果电池已经充满电了,则返回0.如果电池不在充电,或者无法获取到这个时间值,则返回正无穷大.
dischargingTime
只读.一个数字,单位为秒.表示了电池中的电量还剩多长时间会消耗完毕.如果电池正在充电,或者无法获取到这个时间值,以及如果系统没有电池,则返回正无穷大.
level
只读. 一个数字,单位为秒.表示了系统的电池的电量等级,从0到1.0.如果电量已经完全消耗完,则返回0,如果电量为充满状态,或者无法获取到这个等级值,以及如果系统没有电池,则返回1.0.

事件

chargingchange
charging属性值发生改变时触发该事件.
chargingtimechange
chargingTime属性值发生改变时触发该事件.
dischargingtimechange
dischargingTime属性值发生改变时触发该事件.
levelchange
level属性值发生改变时触发该事件.

示例

查看规范中的这个例子.

var battery = navigator.battery || navigator.mozBattery || navigator.webkitBattery;

function updateBatteryStatus() {
  alert("Battery status: " + battery.level * 100 + " %");

  if (battery.charging) {
    alert("Battery is charging"); 
  }
}

battery.addEventListener("chargingchange", updateBatteryStatus);
battery.addEventListener("levelchange", updateBatteryStatus);
updateBatteryStatus();

规范

Specification Status Comment
Battery Status API Candidate Recommendation Initial specification.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
battery
Deprecated
Chrome No support 20 — 29
Prefixed
No support 20 — 29
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge No support NoFirefox No support 16 — 50
Notes
No support 16 — 50
Notes
Notes Removed in favor of navigator.getBattery().
Notes The Battery API was supported on Android, Windows, and Linux with UPower installed. Support for macOS was available starting with Firefox 18.
No support 11 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
No support 10 — 16
Disabled
Disabled From version 10 until version 16 (exclusive): this feature is behind the dom.battery.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android No support 16 — 50
Notes
No support 16 — 50
Notes
Notes Removed in favor of navigator.getBattery().
Notes The Battery API was supported on Android, Windows, and Linux with UPower installed. Support for macOS was available starting with Firefox 18.
No support 14 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
No support 10 — 16
Disabled
Disabled From version 10 until version 16 (exclusive): this feature is behind the dom.battery.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No

Legend

No support  
No support
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

相关链接