JavaScript Battery API
Mozilla Aurora 11 was recently released with a bevy of new features. One of those great new features is their initial implementation of the Battery Status API. This simple API provides you information about the battery's current charge level, its charging status, and allows you to be notified of changes via a few events. Let's check it out!
The battery object is located at window.navigator.battery, but since this is Mozilla's initial offering and the API is not yet cemented, you'll need to use window.navigator.mozBattery. The helpful mozBattery properties include:
charging:Represents if the system's battery is charging. The attribute must be set to false if the battery is discharging, and set to true, if the battery is charging, full, the implementation is unable to report the state, or there is no battery attached to the system, or otherwise.chargingTime:Represents the time remaining in seconds until the system's battery is fully charged.dischargingTime:Represents the time remaining in seconds until the system's battery is completely discharged and the system is about to be suspended.level:Represents the current battery level scaled from 0 to 1.0. The attribute must be set to 0 if the system's battery is depleted and the system is about to be suspended, and to 1.0 if the battery is full, the implementation is unable to report the battery's level, or there is no battery attached to the system.
Events for each of these statuses are provided, including onchargingchange, onchargingtimechange, ondischargingtimechange, and onlevelchange. Basic usage is simple:
// Get the battery!
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery;
// A few useful battery properties
console.warn("Battery charging: ", battery.charging); // true
console.warn("Battery level: ", battery.level); // 0.58
console.warn("Battery discharging time: ", battery.dischargingTime);
// Add a few event listeners
battery.addEventListener("chargingchange", function(e) {
console.warn("Battery charge change: ", battery.charging);
}, false);
battery.addEventListener("chargingtimechange", function(e) {
console.warn("Battery charge time change: ", battery.chargingTime);
}, false);
battery.addEventListener("dischargingtimechange", function(e) {
console.warn("Battery discharging time change: ", battery.dischargingTime);
}, false);
battery.addEventListener("levelchange", function(e) {
console.warn("Battery level change: ", battery.level);
}, false);
I promised a simple API, didn't I? The battery API is the perfect API: simple, effective, and focused!
So why use the battery API? Since many mobile apps live inside a browser wrapper (aren't completely "native"), it's great to have access to system information. Some processes are power-intensive and it may bear warning the user before starting the process that their device is low on battery. Either way, this simple API's true usefulness should become apparent soon!
Comments
Be Heard!
Share your thoughts without being a jerk! And wrap your code in <code> tags, f00!
One simple use case is when you’re having this huge ass survey system, where it could take to over 30 minutes to complete the survey. You might want to check if the user has a charger in place. Even if your application is really good at saving data, it’s still a pain for a user to come back to a long and dreading survey in the first place. Why take any risks? ;-)
You could be saving the data into the browser local storage periodically in this scenario :P
I personally can’t find any uses for this, except maybe for adding some easter egg stuff…
We can use it also for disabling/enabling some javascript features like animations and graphics that eat some enegie depending on the device charges.
Before reading your comment, I had that exact same idea. So I went ahead and made a Modernizr detect for this. It only works in firefox Aurora right now, but maybe it will roll out to more places soon.
https://github.com/Modernizr/Modernizr/blob/master/feature-detects/battery-level.js