js防抖处理

js防抖

let search = document.getElementById('search');

let time = null;

function debounce (handle, delay) {

    let time = null;

    return function () {

        let self = this,arg = arguments;

        clearTimeout(time);

        time = setTimeout(function () {

            handle.apply(self,arg);  //this绑定

        },delay)

    }

}

function ajax (e) {

    console.log(e,this.value)

}

search.oninput = debounce(ajax, 1000)  //1s后发出请求

你可能感兴趣的:(js)