获得徽章 0
赞了这篇文章
赞了这篇文章
赞了这篇文章
赞了这篇文章
赞了这篇文章
赞了这篇文章
赞了这篇文章
赞了这篇文章
赞了这篇文章
赞了这篇文章
赞了这篇沸点
关于函数防抖的几个问题:
1. 为什么要使用闭包,返回一个function呢?
2. 为什么要使用fn.call(_this, _args) 改变this指向,不用的话有什么问题?
function debounce(fn, delayTime) {
let timer = null
return function (args) {
if (timer) {
timer = null
clearTimeout(timer)
}
let _this = this
let _args = args
timer = setTimeout(function () {
fn.call(_this, _args)
}, delayTime)
}
}
let inputDom = document.getElementById('input2')
inputDom.addEventListener('keyup', debounce((e) => {console.log(e.target.value)}, 1000))
1. 为什么要使用闭包,返回一个function呢?
2. 为什么要使用fn.call(_this, _args) 改变this指向,不用的话有什么问题?
function debounce(fn, delayTime) {
let timer = null
return function (args) {
if (timer) {
timer = null
clearTimeout(timer)
}
let _this = this
let _args = args
timer = setTimeout(function () {
fn.call(_this, _args)
}, delayTime)
}
}
let inputDom = document.getElementById('input2')
inputDom.addEventListener('keyup', debounce((e) => {console.log(e.target.value)}, 1000))
展开
3
5
赞了这篇文章
赞了这篇文章
赞了这篇文章