Jump to content

Battery Status

Graham Quince

Recommended Posts

Here's a cute bit of code that shows that Javascript can read your laptop or Android's battery:

    .battery_level {
        font-size: 30px;
        font-weight: bold;
        border: 1px solid #cccccc;
        border-radius: 6px;
        padding: 20px;

<div class="battery_level"></div>

    var batteryDiv = arguments[0].find('.battery_level');
    let isBatterySupported = 'getBattery' in navigator;
    if(!isBatterySupported) {  
        batteryDiv.html('Battery not supported');
    } else {
        let batteryPromise = navigator.getBattery();
        function batteryCallback(batteryObject) {
            var percent = batteryObject.level*100;
            batteryDiv.html('Your battery is at: '+percent+' %');


Probably not of tremendous use, I suppose you could use to remind students to plug in.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...