Loading... ##### 1、代码 ```javascript // 防抖函数 export function debounce (delayTime) { let currentTime = new Date().getTime(); if (currentTime - this.currentDelayTime > delayTime) { this.submit(); } else { console.log("操作频繁"); } this.currentDelayTime = currentTime } ``` ##### 2、使用方法 ```javascript // 防抖函数的使用 // 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 } ``` 最后修改:2024 年 06 月 13 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 1 如果觉得我的文章对你有用,请随意赞赏