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>
<p id="accelerometer">waiting for accelerometer...</p>
<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>
<p id="accelerometer">waiting for accelerometer...</p>
<button onclick="stopwatch();">stop watching</button>
</body>
</html>