Home  >  Article  >  Web Front-end  >  JavaScript method to implement battery status

JavaScript method to implement battery status

coldplay.xixi
coldplay.xixiforward
2020-08-28 16:54:362928browse

JavaScript method to implement battery status

[Related graphic and text recommendations: js tutorial (graphic and text)]

Starting from Mozilla Aurora 11, in Firefox browser Some new features have been implemented, one of which is the basic implementation of the battery status interface. This very simple interface can provide you with information about the current battery level, whether it is charging, and some battery status change events. Let’s see the results!

The battery object is stored in window.navigator.battery, but because this is the first time Firefox has implemented and provided this interface, it is not popular yet, so you need to use window. navigator.mozBatteryThis way of writing. This mozBattery object has the following properties:

  • charging: Indicates whether the current battery device is charging. If the battery is not charged, this value is false. If true, the battery is charging. The current API implementation cannot get information about whether it is full, nor can it determine whether the current device has a battery.
  • chargingTime: refers to how long it takes until the battery is fully charged.
  • dischargingTime: Battery usage time.
  • level: represents the power level, from 0 to 1.0. When this value is 0, it means that the battery is exhausted and the system is about to shut down. If it is 1.0, it means the battery is fully charged.

For these states, the interface provides corresponding events, including onchargingchange, onchargingtimechange, ondischargingtimechange, and onlevelchange. The basic usage is simple:

// 获取电池对象!
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery;

// 显示一些有用属性值
console.warn("电池充电状态: ", battery.charging); // true
console.warn("电量水平: ", battery.level); // 0.58
console.warn("电池使用时间: ", battery.dischargingTime);

// 设置一些事件监听器
battery.addEventListener("chargingchange", function(e) {
	console.warn("电池充电状态变化: ", battery.charging);
}, false);
battery.addEventListener("chargingtimechange", function(e) {
	console.warn("电池充电时间变化: ", battery.chargingTime);
}, false);
battery.addEventListener("dischargingtimechange", function(e) {
	console.warn("电池使用时间变化: ", battery.dischargingTime);
}, false);
battery.addEventListener("levelchange", function(e) {
	console.warn("电量水平变化: ", battery.level);
}, false);

Pretty simple, isn’t it? These interfaces are all great: simple, efficient, and practical!

Why use these battery programming interfaces? Because many mobile applications packaged with browsers (non-native) need to know the current state of the system. Some CPUs are very sensitive to power. The device must have sufficient power before handling certain special tasks. The App should remind the user in advance that the power is low and please charge.

Related learning recommendations: javascript learning tutorial

The above is the detailed content of JavaScript method to implement battery status. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:webhek.com. If there is any infringement, please contact admin@php.cn delete