--- js anti-shake function

After a period of time to implement the provisions execution
    <INPUT type = "text" ID = "InP" /> 
    <Script> var OINP = document.getElementById ( "InP" )
         var Timer = null ; function Ajax (E) {   // function to be executed 
            the console.log ( the this .Value);   
        } 
        oInp.oninput = function (E) { 
            the clearTimeout (timer);   // on the end of a timer var _this = the this , _argm = arguments 
            timer = the setTimeout ( function () {    // after 1000ms execute the timer in the event
        

        
            
                ajax.apply (_this, _argm)    // use this point to apply this function to the 
            }, 1000 ) 
        }
     </ Script>

 Or packaged as a function of the

    <input type="text" id="inp" />
    <script>   
        var oInp = document.getElementById("inp")
        var timer = null;

        function ajax(e) { // 需要执行的函数
            console.log(this.value, e);
        }

        function debounce(handler, delay) { //封装防抖
            var timer = null;
            return function() {
                var _this = this,
                    _argm = arguments;
                clearTimeout(timer)
                timer = setTimeout(function() {
                    handler.apply(_this, _argm)
                }, delay)
            }
        }

        oInp.oninput = debounce(ajax, 1000)  
     </script>

 

Guess you like

Origin www.cnblogs.com/PasserByOne/p/12005742.html