当前位置: 移动技术网 > IT编程>网页制作>CSS > 前端防抖效果实现教程

前端防抖效果实现教程

2018年04月01日  | 移动技术网IT编程  | 我要评论

输入内容后延迟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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网