4 <meta name=
"description" content=
"Test for Battery Status API"/>
5 <title>Battery Status API
</title>
10 <td>Battery Status API
</td>
11 <td width=
"250px"></td>
20 <td id=
"charging"></td>
24 <td id=
"chargingTime"></td>
27 <td>dischargingTime
</td>
28 <td id=
"dischargingTime"></td>
31 <td>battery level
</td>
40 <td>promise status
</td>
41 <td id=
"promiseStatus"></td>
44 <td>number of updates
</td>
45 <td id=
"numberUpdates"></td>
50 var numberUpdates
= 0;
53 function batterySuccess(batteryManager
) {
54 battery
= batteryManager
;
55 document
.getElementById("promiseStatus").innerHTML
= "success";
56 updateBatteryInformation();
57 battery
.addEventListener('chargingchange', updateBatteryInformation
);
58 battery
.addEventListener('chargingtimechange', updateBatteryInformation
);
59 battery
.addEventListener('dischargingtimechange', updateBatteryInformation
);
60 battery
.addEventListener('levelchange', updateBatteryInformation
);
63 function batteryFailure() {
64 document
.getElementById("promiseStatus").innerHTML
= "failed";
67 function ConvertToHMS(durationInSeconds
) {
68 if (!isFinite(durationInSeconds
)) return "";
69 var hours
= Math
.floor(durationInSeconds
/ 3600);
70 var seconds
= durationInSeconds
% 60;
71 var minutes
= Math
.floor((durationInSeconds
- hours
* 3600 - seconds
) / 60);
72 return " (" + hours
+ "h:" + minutes
+ "m:" + seconds
+ "s)";
75 function updateBatteryInformation() {
76 document
.getElementById("charging").innerHTML
= battery
.charging
;
77 document
.getElementById("chargingTime").innerHTML
= battery
.chargingTime
+ ConvertToHMS(battery
.chargingTime
);
78 document
.getElementById("dischargingTime").innerHTML
= battery
.dischargingTime
+ ConvertToHMS(battery
.dischargingTime
);
79 document
.getElementById("level").innerHTML
= battery
.level
;
81 document
.getElementById("numberUpdates").innerHTML
= numberUpdates
;
84 document
.getElementById("promiseStatus").innerHTML
= "pending";
85 document
.getElementById("numberUpdates").innerHTML
= numberUpdates
;
86 navigator
.getBattery().then(batterySuccess
, batteryFailure
);