目录
何谓防抖和节流?
-
防抖
防抖的意思是,在连续的操作中,无论进行了多长时间,只有某一次的操作后在指定的时间内没有再操作,这一次才被判定有效。具体场景可以搜索框输入关键字过程中实时 请求服务器匹配搜索结果,如果不进行处理,那么就是输入框内容一直变化,导致一直发送请求。如果进行防抖处理,结果就是当我们输入内容完成后,一定时间(比如500ms)没有再 输入内容,这时再触发请求。 -
节流
节流的意思是,规定时间内,只触发一次。比如我们设定500ms,在这个时间内,无论点击按钮多少次,它都只会触发一次。具体场景可以是抢购时候,由于有无数人 快速点击按钮,如果每次点击都发送请求,就会给服务器造成巨大的压力,但是我们进行节流后,就会大大减少请求的次数。
结合以上两种情况,回到我们最实际的场景,比如防止表单提交按钮被多次触发,我们应该选择使用节流
而不是防抖
方案。
防抖
/**
* 防抖原理:一定时间内,只有最后一次操作,再过wait毫秒后才执行函数
*
* @param {Function} func 要执行的回调函数
* @param {Number} wait 延时的时间
* @param {Boolean} immediate 是否立即执行
* @return Function
*/
const debounce = (func, wait = 500, immediate = false) => {
let timer
return function () {
// 清除定时器
if (timer) clearTimeout(timer)
if (immediate) {
// 立即执行,此类情况一般用不到
// 如果已经执行过,不再执行
let callNow = !timer
let hasCall = false
timer = setTimeout(() => {
timer = null
if (!hasCall) {
func.apply(this, arguments)
}
}, wait)
if (callNow) {
hasCall = true
func.apply(this, arguments)
}
} else {
// 后置执行
// 设置定时器,当最后一次操作后,timer 不会再被清除,所以在延时wait毫秒后执行func回调方法
timer = setTimeout(() => {
// 函数调用,并将匿名函数的入参传给func
func.apply(this, arguments)
}, wait)
}
}
}
节流
/**
* 节流原理:在一定时间内,只能触发一次
*
* @param {Function} func 要执行的回调函数
* @param {Number} wait 延时的时间
* @param {Boolean} immediate 是否立即执行
* @return Function
*/
const throttle = (func, wait = 500, immediate = true) => {
let startTime = 0
return function () {
// 立即调用
if (immediate) {
let entTime = new Date().getTime()
if (entTime - startTime >= wait) {
func.apply(this, arguments)
startTime = new Date().getTime()
}
} else {
// 延迟调用
setTimeout(() =>{
let entTime = new Date().getTime()
if (entTime - startTime >= wait) {
func.apply(this, arguments)
startTime = new Date().getTime()
}
}, wait)
}
}
}
demo.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>防抖与节流</title>
</head>
<body>
<label for="debounce">输入框 => 防抖:</label><input type="text" id="debounce" />
<br>
<br>
<button class="throttle">提交按钮 => 节流</button>
<script>
/**
* 防抖原理:一定时间内,只有最后一次操作,再过wait毫秒后才执行函数
*
* @param {Function} func 要执行的回调函数
* @param {Number} wait 延时的时间
* @param {Boolean} immediate 是否立即执行
* @return Function
*/
const debounce = (func, wait = 500, immediate = false) => {
let timer
return function () {
// 清除定时器
if (timer) clearTimeout(timer)
if (immediate) {
// 立即执行,此类情况一般用不到
// 如果已经执行过,不再执行
let callNow = !timer
let hasCall = false
timer = setTimeout(() => {
timer = null
if (!hasCall) {
func.apply(this, arguments)
}
}, wait)
if (callNow) {
hasCall = true
func.apply(this, arguments)
}
} else {
// 后置执行
// 设置定时器,当最后一次操作后,timer 不会再被清除,所以在延时wait毫秒后执行func回调方法
timer = setTimeout(() => {
// 函数调用,并将匿名函数的入参传给func
func.apply(this, arguments)
}, wait)
}
}
}
/**
* 节流原理:在一定时间内,只能触发一次
*
* @param {Function} func 要执行的回调函数
* @param {Number} wait 延时的时间
* @param {Boolean} immediate 是否立即执行
* @return Function
*/
const throttle = (func, wait = 500, immediate = true) => {
let startTime = 0
return function () {
// 立即调用
if (immediate) {
let entTime = new Date().getTime()
if (entTime - startTime >= wait) {
func.apply(this, arguments)
startTime = new Date().getTime()
}
} else {
// 延迟调用
setTimeout(() =>{
let entTime = new Date().getTime()
if (entTime - startTime >= wait) {
func.apply(this, arguments)
startTime = new Date().getTime()
}
}, wait)
}
}
}
const debounceDom = document.querySelector('#debounce')
const throttleDom = document.querySelector('.throttle')
const handleInput = (e) => {
const value = e?.target?.value
console.log('value', value);
}
const handleClick = (e) => {
console.log(e);
console.log('提交');
}
debounceDom.addEventListener('input', debounce(handleInput, 400))
// debounceDom.addEventListener('input', debounce(handleInput, 400, true))
throttleDom.addEventListener('click', throttle(handleClick, 400))
// throttleDom.addEventListener('click', throttle(handleClick, 400, false))
</script>
</body>
</html>