-
Notifications
You must be signed in to change notification settings - Fork 1
/
battery.html
39 lines (30 loc) · 1.01 KB
/
battery.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<html>
<head>
<title>Battery Status</title>
</head>
<body>
<h2>What's my battery level?</h2>
<h3 id="battery_level"></h3>
<br>
<h2>How long before my battery runs out?</h2>
<h3 id="battery_discharge"></h3>
<br>
<h2>Is my laptop charging?</h2>
<h3 id="battery_charging"></h3>
</body>
<script>
navigator.getBattery().then(function(battery){
// Displays the current battery level
document.getElementById('battery_level').innerHTML = battery.level * 100 + "%"; // Get value in percentage
// Displays time left before the battery runs out
document.getElementById('battery_discharge').innerHTML = battery.dischargingTime / 60 / 60; // Get time in hours
// Displays if battery is charging (Yes!) or not (No)
document.getElementById('battery_charging').innerHTML = battery.charging ? 'Yes!' : 'No';
// Check for a battery level change
battery.addEventListener('levelchange', function(){
// Do something when battery level changes
alert('Changed');
});
});
</script>
</html>