Created
March 10, 2017 21:26
-
-
Save jeffersonmourak/6f010c20ce195a66b5361e12c6b26beb to your computer and use it in GitHub Desktop.
Battery API Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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