建一个js文件
// 节流
export function throttle(fn, delay = 500) {
let valid = true
return function() {
if (!valid) {
return false
}
valid = false
setTimeout(() => {
fn()
valid = true
}, delay)
}
}
// 防抖
export function debounce(fn, delay = 500) {
let timer = null
return function() {
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, arguments)
timer = null
}, delay)
}
}
以上是封装
this.getData() 比如是搜索按钮
引用
import { debounce } from '@/utils' // 防抖 文件所在的位置
getData:debounce( function() {
请求内容
},可写可不写(不写默认500) )