Skip to content

Commit

Permalink
Update the battery example to match the spec
Browse files Browse the repository at this point in the history
Uses the `navigator.getBattery()` promise when available instead of the deprecated `navigator.battery`
  • Loading branch information
Léo-Paul COUTURIER committed Nov 3, 2014
1 parent 23b00d1 commit e2208e9
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion battery/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ function log(message){
document.querySelector("#data").innerHTML += message + "<br />";
}

if (battery) {
function logBattery(battery) {
log("Battery level: " + battery.level);
log("Battery charging: " + battery.charging);
log("Battery discharging time: ", battery.dischargingTime);
battery.addEventListener("chargingchange", function(e) {
log("Battery chargingchange event: " + battery.charging);
}, false);
}

if(navigator.getBattery) {
navigator.getBattery().then(logBattery, function() {
log("There was an error while getting the battery state.");
});
} else if (battery) {
logBattery(battery);
} else {
log("Shame! The Battery API is not supported on this platform.")
}

0 comments on commit e2208e9

Please sign in to comment.