HTML, Battery Level |
HTML, Battery Level |
s1lv3rf0x87 |
Jan 18 2017, 06:38 AM
Post
#1
|
Group: Members Posts: 5 Joined: 18-January 17 Member No.: 26,270 |
Hey Guys,
I have some HTML code which will display the battery status of a device and display the charge as a decimal value. I am trying to tweak the code so it displays the charge value as a percentage and not a decimal value. Can someone have a look at this and advise if this is possible and if so how to do this. Many Thanks in advance. CODE <html> <head> <title>Battery Status API Example</title> <script> window.onload = function () { function updateBatteryStatus(battery) { document.querySelector('#charging').textContent = battery.charging ? 'charging' : 'not charging'; document.querySelector('#level').textContent = battery.level; document.querySelector('#dischargingTime').textContent = battery.dischargingTime / 60; } navigator.getBattery().then(function(battery) { // Update the battery status initially when the promise resolves ... updateBatteryStatus(battery); // .. and for any subsequent updates. battery.onchargingchange = function () { updateBatteryStatus(battery); }; battery.onlevelchange = function () { updateBatteryStatus(battery); }; battery.ondischargingtimechange = function () { updateBatteryStatus(battery); }; }); }; </script> </head> <body> <div id="charging">(charging state unknown)</div> <div id="level">(battery level unknown)</div> <div id="dischargingTime">(discharging time unknown)</div> </body> </html> |
Lo-Fi Version | Time is now: 28th March 2024 - 09:59 AM |