I will often pay attention to the development of new JavaScript APIs in various browsers. One of the APIs is the battery status information API. I wrote an article about the battery status information API before, when this API was still new to various browsers. However, with the development of technology, this battery status information API has been changed and upgraded. It has now become a promise-based API, and you can use it in the latest version of Google Chrome. Let's take a look at the usage of this latest API!
This API is still provided by the navigator
object, but now it is called getBattery
:
navigator.getBattery().then(function(result) {});
By calling getBattery()
call, we get a promise, this promise will call back the then
method and provide a BatteryManager
object. The following is a description of this BatteryManager
object:
// result: BatteryManagery { charging: false, chargingTime: Infinity, dischargingTime: 8940, level: 0.59, onchargingchange: null, onchargingtimechange: null, ondischargingtimechange: null, onlevelchange: null }
The way this new API is executed is somewhat different, but the results returned are very similar. Remember the usage of the old Battery Status API, which had some issues with Mac device batteries, and the new API didn't make any difference. Using browser engine prefixes is a solution, but it's just disgusting. We hope that all browsers will be upgraded as soon as possible to give programmers a better programming experience.
Recommended tutorial: "javascript basic tutorial"
The above is the detailed content of JavaScript Battery Status Information API Promises. For more information, please follow other related articles on the PHP Chinese website!