functiondeviceMotionHandler(eventData) { var acceleration = eventData.accelerationIncludingGravity, x, y, z; x = acceleration.x; y = acceleration.y; z = acceleration.z; document.getElementById("status").innerHTML = "x:"+x+"<br />y:"+y+"<br />z:"+z; }
“摇一摇”的动作既“一定时间内设备了一定距离”,因此通过监听上一步获取到的x, y, z 值在一定时间范围内 的变化率,即可进行设备是否有进行晃动的判断。而为了防止正常移动的误判,需要给该变化率设置一个合适的临界 值。
functiondeviceMotionHandler(eventData) { var acceleration = eventData.accelerationIncludingGravity; var curTime = newDate().getTime();
if ((curTime - last_update) > 100) { var diffTime = curTime - last_update; last_update = curTime; x = acceleration.x; y = acceleration.y; z = acceleration.z; var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000; var status = document.getElementById("status");