Starting from Mozilla Aurora 11, Firefox browser has implemented some new features, 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 power, whether it is charging, etc., as well as some battery status changes. Let's see the effect!
The battery object is stored in it, but because this is the first time Firefox browser has implemented and provided this interface, it is not popular, and you need to use this writing method. This mozBattery object has the following properties:
: 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 obtain information on whether it is full, nor can it be determined whether the current device has a battery.
: It refers to how long it will take until the battery is fully charged.
: Battery has been used.
: Indicates the power level, from 0 to 1.0. When this value is 0, it means that the power 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 respective corresponding events, including onchargingchange, onchargingtimechange, ondischargingtimechange, and onlevelchange. The basic usage is very simple:
// Get the battery object!
var battery = || || ;
// Show some useful attribute values
("Battery Charge Status: ", ); // true
("Power level: ", ); // 0.58
("Battery usage time: ", );
// Set some event listeners
("chargingchange", function(e) {
("Battery charging status change: ", );
}, false);
("chargingtimechange", function(e) {
("Battery charging time change: ", );
}, false);
("dischargingtimechange", function(e) {
("Battery usage time change: ", );
}, false);
("levelchange", function(e) {
("Power level change: ", );
}, false);
Very simple, isn't it? These interfaces are very good: simple, efficient and practical!
Why use these battery programming interfaces? Because many browser-encapsulated mobile applications (non-'native') need to know the current status of the system. Some CPUs are very sensitive to power. Before handling certain special tasks, the equipment must have sufficient power. The app should remind the user in advance that the battery is insufficient and please charge it.