Get battery status using HTML5 Battery Status API

This code shows how to determine that the battery is low and perform the required action. as the charge-saving actions: use power APi to change screen state, disable ccs and js animation, stopping ajax polls, storing data to webStorage, disable sound and vibration,using AppCache.
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery || navigator.msBattery;

if (battery) {
	// battery API supported
	var isNormal = (battery.charging || battery.level > 0.15);
	
	battery.onlevelchange = function() {
		
		var temp = isNormal;
		isNormal = (battery.charging || battery.level > 0.15);
		
		if (isNormal != temp) {
			if (isNormal) {
				console.log( "Battery power is OK." );
			}
			else {
				console.log( "Battery power is critical." );
				doAction();
			}
		}

	}
}

Responses

0 Replies