Working at Mozilla has afforded me the time to look at new APIs, one of them being the Battery API. I wrote about the Battery API a while back, when the API was fresh into browsers. Since then the Battery API has changed, implementing a promise-based API which is now available only in Google Chrome. Let's have a look at how to use the new API!
在Mozilla的工作使我有时间研究新的API,其中之一就是Battery API 。 不久以前,当API在浏览器中崭露头角时,我就写了Battery API 。 从那时起,Battery API发生了变化,实现了基于承诺的API,现在仅在Google Chrome中可用。 让我们看看如何使用新的API!
View Demo 观看演示The functionality we need is still hosted on the navigator object, but this time it's a function called getBattery:
我们所需的功能仍托管在navigator对象上,但这一次它是一个名为getBattery的函数:
navigator.getBattery().then(function(result) {});A promise is returned from the getBattery() call, and the callback provided to then is given a BatteryManager object which provides the following example information:
从getBattery()调用返回一个promise, then提供给回调提供一个BatteryManager对象,该对象提供以下示例信息:
// result: BatteryManagery { charging: false, chargingTime: Infinity, dischargingTime: 8940, level: 0.59, onchargingchange: null, onchargingtimechange: null, ondischargingtimechange: null, onlevelchange: null } View Demo 观看演示The new API is different in execution but likewise in result. Remember that the old API had issues with Mac device batteries, and that doesn't seem to have changed. Accommodating for different browser prefixes is a pain but something we've come to accept, but it really sucks when the methodology is completely different. Let's hope that other browser vendors are quick to update and we can see a unified, browser-prefixed (banter) API again.
新的API在执行方面有所不同,但结果也相同。 请记住,旧的API在Mac设备电池上存在问题,并且似乎并没有改变。 适应不同的浏览器前缀是一件很痛苦的事情,但是我们已经接受了这一点,但是当方法完全不同时,这确实很麻烦。 希望其他浏览器供应商能够快速更新,并且我们可以再次看到统一的,以浏览器为前缀的( 戏ter )API。
翻译自: https://davidwalsh.name/javascript-battery-api
相关资源:jdk-8u281-windows-x64.exe