在公司业务中,我们可能经常会用到防抖函数来优化减少后端压力
防抖指的是 , 用户连续点击过程过程中不生效 , 用户停止点击动作时候生效的一种优化减少连续请求服务器的一种性能优化手段 , 我们可以在公共方法文件中,创建公共的防抖函数 , 在实际业务页面中使用即可实现
- 公共防抖函数封装
/**
* @param {Fn} func
* @param {number} wait
* @returns {Fn}
*/
export function debounce(func, wait = 1500) {
let timeout;
return function (event) {
clearTimeout(timeout)
timeout = setTimeout(() => {
func.call(this, event)
}, wait)
}
}
- 页面中的使用
函数名:debounce(function(){},200)