我正在使用由PhoneGap编译并部署到Android的示例应用程序。明显滞后-我的刷新时间设置为100毫秒,但是您可以感觉到加速度读数仅以“跳跃”的方式每1-2秒更新一次。该手机是运行android 2.xx OS的单核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更改string可获得更好的性能。

查看这个jsperf:http://jsperf.com/innerhtml-perf

关于javascript - 适用于Android的PhoneGap加速度计应用程序像疯了似的滞后,我的代码有什么问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9335071/

10-09 07:14
查看更多