其他分享
首页 > 其他分享> > JS防抖

JS防抖

作者:互联网

JS防抖

        let inp = document.getElementById('input') //获取dom元素         function bandence(fn, delay) {             let n = null //利用闭包             return function () {                 if (n !== null) {                     clearTimeout(n)                 }                 n = setTimeout(() => {                     // console.log(this)                     fn.call(this) //利用call 该变this指向由window改为inp对象调用fn函数                 }, daly);             }         }         inp.oninput = bandence(function () {             console.log(this.value) //业务逻辑代码         }, 500)   利用定时器实现防抖,delay为业务代码延迟时间。  

标签:function,防抖,inp,JS,null,fn
来源: https://www.cnblogs.com/GFM0518/p/16577532.html