Last active
May 25, 2018 14:58
-
-
Save rbrahul/aeab556179ab424d496ee66192d9f184 to your computer and use it in GitHub Desktop.
Javascirpt Battery Status Browser API
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
function updateChargeInfo(battery) { | |
var message = battery.charging ? "Charging now :D": "Need Charge" ; | |
var batteryStatusNode = document.querySelector('.charging-status'); | |
var classes = batteryStatusNode.classList; | |
document.querySelector('#message').innerHTML =message; | |
if(battery.charging) { | |
if (classes.contains('charging') === false) { | |
classes.add('charging'); | |
classes.remove('not-charging'); | |
} | |
} else { | |
classes.remove('charging'); | |
classes.add('not-charging'); | |
} | |
} | |
navigator.getBattery().then(battery => { | |
updateChargeInfo(battery); battery.addEventListener('chargingchange', function(){ | |
updateChargeInfo(battery); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment