Articles → JavaScript → Check if battery of your laptop is charging or not using battery status API in JavaScript

Check if battery of your laptop is charging or not

using battery status API in JavaScript






navigator.GetBattery()





Onchargingchange event





Example




<!DOCTYPE html>
<html>

<head>
	<title>Battery Status API Example</title>
	<script>
	window.onload = function() {
		navigator.getBattery().then(function(battery) {
			// Update the battery status initially when the promise resolves ...
			document.getElementById('charging').textContent = battery.charging ? 'charging' : 'not charging';
			// .. and for any subsequent updates.
			battery.onchargingchange = function() {
				document.getElementById('charging').textContent = battery.charging ? 'charging' : 'not charging';
			};
		});
	};
	</script>
</head>

<body>
	<div id="charging">(charging state unknown)</div>
</body>

</html>



Output



Click to Enlarge

Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, October 30, 2018

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250