怎么用 JavaScript 禁止 devicemotion 事件

爱许乐
  • 179

怎么写禁止摇一摇函数? function shakeStop();

 
        var SHAKE_THRESHOLD = 1000;
        var last_update = 0;
        var x = y = z = last_x = last_y = last_z = 0;

        
        if (window.DeviceMotionEvent) {
            window.addEventListener('devicemotion', deviceMotionHandler, false);
        } else {
            alert('本设备不支持devicemotion事件');
        }
        
        function deviceMotionHandler(eventData) {
            var acceleration = eventData.accelerationIncludingGravity;
            var curTime = new Date().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");
        
                if (speed > SHAKE_THRESHOLD) {
                    doResult();
                }
                last_x = x;
                last_y = y;
                last_z = z;
            }
        }

        function doResult() {
            //这里是弹窗  当弹窗弹出来,就禁止继续摇一摇,怎么写?
            
            congratulationTc();
            audio2.play();
            audio1.pause();
            $('.god_animation').removeClass('zindex');
     
        }
回复
阅读 2.1k
2 个回答

removeEventListener


        
        var deviceMotionUtil=(function(){
            var SHAKE_THRESHOLD = 1000;
            var last_update = 0;
            var x = y = z = last_x = last_y = last_z = 0;

            
            function disable(){
                window.removeEventListener('devicemotion', deviceMotionHandler, false);
            }
            
            function enable(){
                if (window.DeviceMotionEvent) {
                    window.addEventListener('devicemotion', deviceMotionHandler, false);
                    return true;
                } else {
                    alert('本设备不支持devicemotion事件');
                    return false;
                }
            }
            function deviceMotionHandler(eventData) {
                var acceleration = eventData.accelerationIncludingGravity;
                var curTime = new Date().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");
            
                    if (speed > SHAKE_THRESHOLD) {
                        doResult();
                    }
                    last_x = x;
                    last_y = y;
                    last_z = z;
                }
            }
            return {
                disable:disable,
                enable:enable            
               
              }
        }());
        
        
        function doResult() {
            //这里是弹窗  当弹窗弹出来,就禁止继续摇一摇,怎么写?
            deviceMotionUtil.disable();
            congratulationTc();
            audio2.play();
            audio1.pause();
            $('.god_animation').removeClass('zindex');
     
        }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏