HTML and CSS Reference
I.3.6. Battery API: adjusting application processing based on battery life
The Battery API allows you to adjust how much processing your app does depending on
the state of the battery. In a real app, you could avoid doing any heavy processing or reduce
the number of network connections when the battery is low. In our example app, there isn't
much opportunity to cut back processing, so you're just going to draw less of Wilson as the
battery level drops. Figure I.16 shows the end result in Firefox on an Android phone.
Figure I.16. By integrating the Battery API, you can make your app do less work as the charge level drops.
The Battery API consists of four properties and four events. See the summary in table I.7 .
Table I.7. The Battery API
Is the power connected?
Seconds remaining until the battery is charged.
Seconds remaining until the battery is discharged.
A value between 0.0 and 1.0 representing the current bat-
tery charge level, where 1.0 is full.
The value of charging has changed.
The chargingTime has changed.
The dischargingTime has changed.
The level has changed.
In this example you're just going to take advantage of the charging and level prop-
erties. The following table shows the browser compatibility; this API will work on mobile
devices but also laptops.