Battery Status

One of the key concerns with portable devices is knowing their battery status. Mobile devices can get as little as seven or eight hours out of a full charge, whereas a laptop is lucky to get more than three or four hours. Knowing the status of the device’s battery can be important before you begin power-hungry processes or commence to download large files.

You can get information about the battery with the Battery Status API, which brings a set of attributes on the navigator.battery object. For example, to find out if the battery is currently charging, you can use the charging attribute to get a true or false value:

var batteryStatus = navigator.battery.charging;

To find the current battery level, you can use the level attribute, ...

Get The Modern Web now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.