PhoneGap-----Accelerometer
Everything in the code!!!!
[html]
<!DOCTYPE html>
<head>
<title>Acceleration Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
var watchID = null;
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady()
{
startWatch();
}
function startWatch()
{
var options = {frequency:3000};
watchID = navigator.accelerometer.watchAcceleration(onSuccess,onError,options);
}
function stopWatch()
{
if(watchID)
{
navigator.accelerometer.clearWatch(watchID);
watchID = null;
}
}
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 />';
}
function onError()
{
alert('oooops!');
}
</script>
</head>
<body>
<div id="accelerometer">Waiting for accelerometer...</div>
<button onClick="stopWatch();">Stop Watching</button>
</body>
</html>
<!DOCTYPE html>
<head>
<title>Acceleration Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
var watchID = null;
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady()
{
startWatch();
}
function startWatch()
{
var options = {frequency:3000};
watchID = navigator.accelerometer.watchAcceleration(onSuccess,onError,options);
}
function stopWatch()
{
if(watchID)
{
navigator.accelerometer.clearWatch(watchID);
watchID = null;
}
}
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 />';
}
function onError()
{
alert('oooops!');
}
</script>
</head>
<body>
<div id="accelerometer">Waiting for accelerometer...</div>
<button onClick="stopWatch();">Stop Watching</button>
</body>
</html>
补充:web前端 , HTML 5 ,