Home > Web Front-end > JS Tutorial > JavaScript Battery Status Information API Promises

JavaScript Battery Status Information API Promises

coldplay.xixi
Release: 2020-08-28 16:56:30
forward
2639 people have browsed it

JavaScript Battery Status Information API Promises

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) {});
Copy after login

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
}
Copy after login

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!

Related labels:
source:webhek.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template