/!\window.navigator.battery 的使用 IS STRONGLY DISCOURAGED这个问题现在不值得检查,谢谢/!\
我想获取当前系统的电池电量。这是我想要得到的输出:
如果是台式机(无电池) 电池电量:100%
如果是笔记本电脑 电池电量:XXX%(取决于电量)
所以我在 window.navigator.battery
上尝试了 Mozilla 基金会的片段在我的 JSFiddle .
问题: 运行 Chrome 20+ 版本(目前是 46),控制台仍然报错:
Uncaught TypeError: Cannot read property 'addEventListener' of undefined
所以我的理解是用假定的电池电量实例化的对象 battery
没有返回任何内容。
有人已经解决了这个问题吗?
当此代码段起作用时,我的算法要点是定义用户不能针对剩余电池电量执行的操作。例如,我不希望用户执行可能会使他的电池电量降低到临界点并使关键操作失败的重 Action 。
最佳答案
来自English version of that page :
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.
和
The
battery
property is deprecated and has been replaced by aNavigator.getBattery()
method returning a batteryPromise
. Its support is still partial though.
关于javascript - 获取电池电量 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521209/