1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| var SHAKE_THRESHOLD = 3000;
var last_update = 0,x,y,z,last_x,last_y,last_z;
yyopen = 1;
function deviceMotionHandler(eventData){ var acceleration = eventData.accelerationIncludingGravity; var curTime = new Date().getTime(); var diffTime = curTime - last_update; if (diffTime > 100){ last_update = curTime, x = acceleration.x, y = acceleration.y, z = acceleration.z; var speed = Math.ceil(Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000); if(speed > SHAKE_THRESHOLD){ if(yyopen >= 1){ $("#main").html(speed+','+ ++yyopen) } } } last_x = x,last_y = y,last_z = z; } $(function(){ if (window.DeviceMotionEvent){ window.addEventListener('devicemotion', deviceMotionHandler, false); }else{ } })
|