Javascript Battery API returning Infinite discharging time

433 Views Asked by At

It is the wish of every smartphone user if it could come true but unfortunatly this doesn't represent reality. Meanwhile... I'm getting that the battery.dischargingTime of my android phone is Inifinite...! (and I wish it was, but it isn't)

This is my code:

if(navigator.getBattery){
    var battery = navigator.getBattery().then(function(battery){
        var level = battery.level;
        var rest = battery.dischargingTime;
    });
}

After that, level is 0.94, this is correct, but rest is Infinite. I could understand it didn't work for my desktop computer but on mobile...! I'm using an android with Chrome 43 btw.

EDIT: I also noticed that in chrome for iPhone this is not even passing the test for the support, despite caniuse not mentioning it: http://caniuse.com/#search=battery

1

There are 1 best solutions below

4
On BEST ANSWER

This value is Infinity if the battery is currently charging rather than discharging, or if the system is unable to report the remaining discharging time.

https://developer.mozilla.org/en-US/docs/Web/API/BatteryManager/dischargingTime

It basically says "sorry, dunno". Ignore that value as invalid and check back again later.