1 //监听手机摇动事件 2 if (window.DeviceMotionEvent) { 3 window.addEventListener(\'devicemotion\', deviceMotionHandler, false); 4 } else { 5 alert(\'你的设备不支持DeviceMotion事件\'); 6 }
1 var SHAKE_THRESHOLD = 3000; 2 var last_update = 0; 3 var x = y = z = last_x = last_y = last_z = 0; 4 //摇一摇开关,1表示开,0表示关 5 var canShake = 1; 6 7 function deviceMotionHandler(eventData) { 8 var acceleration = eventData.accelerationIncludingGravity; 9 var curTime = new Date().getTime(); 10 11 //100ms监听一次,拒绝重复监听 12 if ((curTime - last_update) > 100 && canShake==1) { 13 var diffTime = curTime - last_update; 14 last_update = curTime; 15 x = acceleration.x; 16 y = acceleration.y; 17 z = acceleration.z; 18 var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000; 19 if (speed > SHAKE_THRESHOLD) { 20 canShake=0; 21 var myAuto = document.getElementById(\'musicBox\'); 22 myAuto.play(); 23 24 var httpurl = contextPath + "/servlet/chouJiangServlet?" + Math.random(); 25 $.post(httpurl, { 26 "requestKey" : \'\', 27 "choujiangProgram" : \'Yaoyiyao\', 28 \'choujiangGiftId\' : document.getElementById("choujiangGiftId").value 29 }, function(data) { 30 try { 31 var json = eval("(" + data + ")"); 32 }catch (e) { 33 alert("网络通信故障,请刷新后再试。"); 34 return; 35 } 36 var status = json[\'status\']; 37 var message = json[\'message\']; 38 var awardId = json[\'data\'][\'id\']; 39 40 var yaoyiyaoJifen = json[\'data\'][\'currUserJifen\']; 41 document.getElementById("currUserJifen").value=yaoyiyaoJifen; 42 43 if (status == 1){ 44 switch(awardId){ 45 case 0: 46 document.getElementById("interface1").style.display="none"; 47 document.getElementById("interface2").style.display="inline"; 48 49 break; 50 case 1: 51 document.getElementById("interface1").style.display="none"; 52 document.getElementById("interface3").style.display="inline"; 53 break; 54 } 55 56 //没有登录或登录超时,跳转登录 57 }else if(status == 3) { 58 loading.goUrl("../user/login.shtml"); 59 60 //其他错误 61 }else{ 62 alert(message); 63 } 64 }); 65 } 66 last_x = x; 67 last_y = y; 68 last_z = z; 69 } 70 }