这是代码。基本上,当我点击应用程序没有任何反应(但它应该停止加速计)。这是我认为的JS/DOM问题。JavaScript onMouseClick不起作用?
<!DOCTYPE html>
UNH BSApp
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// The watch id references the current `watchAcceleration`
var watchID = null;
// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
startWatch();
}
function onMouseClick() {
a stopWatch();
}
// Start watching the acceleration
//
function startWatch() {
// Update acceleration every 0.1 seconds
var options = { frequency: 10 };
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('onError!');
}
</script>
<style>
#start {
display:block;
border:solid;
}
</style>
</head>
<body>
<div id="accelerometer">Waiting for accelerometer...</div>
<div id="start">Start</div>
</body>
</html>
你还没有注册该事件点击事件。你刚刚创建了一个处理程序。您还需要注册该事件。 – bhups 2012-02-22 18:03:46
你在哪里注册回调?此外,该事件不是onmouseclick。这是onmousedown,或onmouseup,或onclick。 :) – Akshay 2012-02-22 18:04:13