我正在使用由 PhoneGap 编译并部署到 Android 的示例应用程序。明显滞后 - 我的刷新时间设置为 100 毫秒,但您可以感觉到加速度读数仅以相当“跳跃”的方式每 1-2 秒更新一次。该手机是一款运行 android 2.xx 操作系统的单核 HTC 手机。 (没有更多详细信息 - 目前没有手机)。
加速示例
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
var watchID = null;
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready, start watching
function onDeviceReady() {
startWatch();
}
// Start watching the acceleration
function startWatch() {
// Update acceleration every 3 seconds
var options = { frequency: 100};
watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError,
options);
}
// Stop watching the acceleration
function stopWatch() {
if (watchID) {
navigator.accelerometer.clearWatch(watchID);
watchID = null;
}
}
// onSuccess: Get a snapshot of the current acceleration
function onSuccess(acceleration) {
var element = document.getElementById('accelerometer');
element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
'Acceleration Y: ' + acceleration.y + '<br />' +
'Acceleration Z: ' + acceleration.z + '<br />' +
'Timestamp: ' + acceleration.timestamp + '<br />';
}
// onError: Failed to get the acceleration
//
function onError() {
alert('oooops!');
}
</script>
等待加速计... 停止观看
最佳答案
我不知道这是否有帮助,但似乎仅使用值而不是整个字符串
更改innerHTML
可以获得更好的性能。
看看这个 jsperf:http://jsperf.com/innerhtml-perf
关于javascript - 适用于 Android 的 PhoneGap 加速计应用程序疯狂滞后,我的代码出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9335071/