<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?

有帮助吗?

解决方案

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)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top