Vue2自定义防抖函数
1、代码
// 防抖函数
export function debounce (delayTime) {
let currentTime = new Date().getTime();
if (currentTime - this.currentDelayTime > delayTime) {
this.submit();
} else {
console.log("操作频繁");
}
this.currentDelayTime = currentTime
}
2、使用方法
// 防抖函数的使用
// 1、导入防抖函数
import { debounce } from "存放路径";
// 2、在响应式数据中声明记录当前时间的值(默认赋值为延迟时间)currentDelayTime
// 3、调用函数,传递防抖间隔时间(以2000为例)
debounce(2000){
// 当前时间
let currentTime = new Date().getTime();
// 当前时间减去被记录的当前时间大于延迟时间
if (currentTime - this.currentDelayTime > 2000) {
// 调用执行函数
this.submit();
} else {
// 控制台打印"操作频繁"
console.log("操作频繁");
}
// 将当前时间赋值给记录时间的值
this.currentDelayTime = currentTime
}
以上就是这个简单的函数了,烦请各位多多指教