JavaScripting

The definitive source of the best
JavaScript libraries, frameworks, and plugins.


  • ×

    Battery.js

    A tiny wrapper for the HTML5 Battery Status API.
    Filed under 

    • 🔾6%Overall
    • 118
    • 247.6 days
    • 🕩25
    • 👥2

    Battery.js NPM version NPM downloads MIT License

    A tiny JavaScript wrapper for the HTML5 Battery Status API.

    As of October 2015 the Battery Status API is supported by Firefox, Chrome, Opera, Android Browser and Chrome for Android: http://caniuse.com/battery-status

    Due to privacy concerns, support for the Battery Status API has been dropped from most browsers. See: https://caniuse.com/#feat=battery-status.

    Install

    $ npm install battery.js
    

    Usage

    var Battery = require('battery.js'); // or similar
    
    function logBatteryStatus(status) {
      console.log('Level: ' + Math.floor(status.level * 100) + '%'); // 30%
      console.log('Charging: ' + status.charging);                   // true
      console.log('Time until charged: ' + status.chargingTime);     // 3600 (seconds) or Infinity
      console.log('Battery time left: ' + status.dischargingTime);   // 3600 (seconds) or Infinity
    }
    
    // Get battery status as soon as it's available
    Battery.getStatus(function(status, error) {
      if(error) {
        console.error('Battery status is not supported');
        return;
      }
    
      logBatteryStatus(status);
    });
    
    // Register a handler to get notified when battery status changes
    Battery.onUpdate(logBatteryStatus);
    

    Demo

    pstadler.dev/battery.js

    Show All