当前位置: 移动技术网 > IT编程>网页制作>Html5 > PhoneGap-----Accelerometer

PhoneGap-----Accelerometer

2018年09月21日  | 移动技术网IT编程  | 我要评论

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>


 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网