<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>

input输入框延时防抖

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-28
  • 2021-11-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-04
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案