您现在的位置:首页 >> 前端 >> 内容

前端防抖效果实现教程

时间:2018/2/21 10:07:02 点击:

  核心提示:输入内容后延迟1秒发送请求input type=text id=input /scriptvar input = document.getElementById(input);function deb...

输入内容后延迟1秒发送请求

<input type="text" id="input" />

<script>

    var input = document.getElementById('input');

    function debounce(handler, delay) {
        var timer = null;
        return function () {
            var _salf = this,
                _arg = arguments;
            clearTimeout(timer);
            timer = setInterval(function () {
                handler.apply(_salf, _arg);
            }, delay);
        }
    }

    function ajax() {
        console.log(this.value);
    }
    input.oninput = debounce(ajax, 1000);
</script>

作者:网络 来源:ppSilence的