鍍金池/ 問答/HTML/ 怎么用 JavaScript 禁止 devicemotion 事件

怎么用 JavaScript 禁止 devicemotion 事件

怎么寫禁止搖一搖函數(shù)? 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() {
            //這里是彈窗  當彈窗彈出來,就禁止繼續(xù)搖一搖,怎么寫?
            
            congratulationTc();
            audio2.play();
            audio1.pause();
            $('.god_animation').removeClass('zindex');
     
        }
回答
編輯回答
苦妄

removeEventListener

2017年6月28日 13:13
編輯回答
朕略萌

        
        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() {
            //這里是彈窗  當彈窗彈出來,就禁止繼續(xù)搖一搖,怎么寫?
            deviceMotionUtil.disable();
            congratulationTc();
            audio2.play();
            audio1.pause();
            $('.god_animation').removeClass('zindex');
     
        }
2017年8月9日 10:25