Question

<script>
function onbattery() {
window.addEventListener("batterystatus", onBatteryStatus, false);
document.getElementById('log1').innerHTML+="clicked";       
}

function onBatteryStatus(info) {

document.getElementById('log2').innerHTML+="get";
document.getElementById('getbatstatus').innerHTML+="Level: " + info.level + " isPlugged: "     + info.isPlugged;
}
</script>

<p id="log1">will display log1</p>
<p id="log2">will display log2</p>
<p id="getbatstatus">will display battery status</p>

Above code will display current battery status. On 1st click it showing very fast. But when i want to run it again..it will execute very slowly... why is this happen?

Was it helpful?

Solution

The reason why is the batterystatus event only occurs when the battery status changes (power level increases, decreases or you plug your device in)

The first time you call onbattery, it gives the current status but then it waits for the system to raise the batterystatus event (could be a few minutes before change if you have a string battery)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top