Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jeffersonmourak/6f010c20ce195a66b5361e12c6b26beb to your computer and use it in GitHub Desktop.

Select an option

Save jeffersonmourak/6f010c20ce195a66b5361e12c6b26beb to your computer and use it in GitHub Desktop.
Battery API Example
navigator.getBattery().then(battery=>{
function updateAllBatteryInfo() {
updateChargeInfo();
updateLevelInfo();
updateChargingInfo();
updateDischargingInfo();
}
updateAllBatteryInfo();
battery.addEventListener('chargingchange', function() {
updateChargeInfo();
});
function updateChargeInfo() {
console.log("Battery charging? " + (battery.charging ? "Yes" : "No"));
}
battery.addEventListener('levelchange', function() {
updateLevelInfo();
});
function updateLevelInfo() {
console.log("Battery level: " + battery.level * 100 + "%");
}
battery.addEventListener('chargingtimechange', function() {
updateChargingInfo();
});
function updateChargingInfo() {
console.log("Battery charging time: " + battery.chargingTime + " seconds");
}
battery.addEventListener('dischargingtimechange', function() {
updateDischargingInfo();
});
function updateDischargingInfo() {
console.log("Battery discharging time: " + battery.dischargingTime + " seconds");
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment