How can I get device battery level in javascript

I make a network call every 15 seconds in my application, and if the battery level for users is less than 20%, instead I would use this call every 30 seconds. How to get user devices at current battery level? Is it possible? Any help would be appreciated.

+7
javascript battery
source share
1 answer

Take a look at the battery status API . It does not work in all browsers, but it starts.

For browsers that support it, something like this should work:

navigator.getBattery().then(function(battery) { battery.addEventListener('levelchange', function() { // Do stuff when the level changes, you can get it // from battery.level document.write((battery.level*100)+"%"); }) document.write((battery.level*100)+"%"); }); 
+4
source share

All Articles