<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>Accelerometer Example</title>
<script type="text/javascript">
//监听plusready事件
var xhrs_wid=‘‘;//外部声明一个变量用于给 setTimeout使用,不然xhrs_wid就是addEventListener函数内的局部变量了
document.addEventListener("plusready",xhrs);//系统准备好后执行 自定义函数xhrs
function xhrs(){
xhrs_wid = plus.accelerometer.watchAcceleration(
function(xhrs_a){
document.getElementById("aa").innerHTML = Math.random();//
},function(){document.getElementById("aa").innerHTML = ‘无法获取加速信息‘;},
{frequency:2000}//2秒一次的执行该函数,默认为500
);
}//watchAcceleration方法调用完后会有一个返回值
//watchAcceleration(成功获取加速信息的回调函数有参数【必须】,获取加速信息失败时调用的函数【可选】, {frequency:执行评率毫秒计时} )
//watchAcceleration类似于setInterval
setTimeout(function(){plus.accelerometer.clearWatch(xhrs_wid)},5000);//5秒后关闭加速计算
</script>
</head>
<body>
<div id="aa">1321331231</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>Accelerometer Example</title>
<script type="text/javascript">
document.addEventListener("plusready",xhrs);//监听plusready事件
function xhrs(){
plus.accelerometer.watchAcceleration(
function(xhrs_a){
document.getElementById("aa").innerHTML = "x轴方向的加速度="+xhrs_a.xAxis;
},
function(){document.getElementById("aa").innerHTML = ‘无法获取加速信息‘;},
{frequency:2000}
);
}
</script>
</head>
<body>
<div id="aa"><!--在此处显示加速信息--></div>
</body>
</html>