input输入框延时防抖


    <body>
        <div class="firstBox">
            <div class="box">
                <div class="btn"></div>
                <input type="text" id="cph" value="" />
            </div>
            <div class="content">
            </div>
        </div>
    </body>
    <script type="text/javascript">
        var oInp = document.getElementById("cph");

        oInp.oninput = debounce(ajax, 1000);

        function debounce(handler, delay) {
            var timer = null;
            // 返回函数引用
            return function() {
                var _self = this;
                var _args = arguments;
                clearTimeout(timer);
                timer = setTimeout(function() {
                    /*改变handler的this指向。
                     * handler的this指向当前返回的函数引用,例如:oInp.oninput = debounce(ajax, 1000);
                     * 此时this就是指向oInp.oninput了
                     */
                    handler.apply(_self, _args);
                }, delay);
            }
        }

        function ajax(e) {
            console.log(e + " " + this.value);
        }
    </script>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM