在Vue中全局定义防抖函数【debounce】

    技术2022-07-20  69

    在Vue中全局定义防抖函数

    防抖作用:如果一个函数持续地触发,那么只有在它结束后一定时间才会执行一次。

    首先可以在Vue项目工具JS文件中定义,后续可以引入使用

    export const debounce = function (fn, delay) { let timer = null; return function () { let context = this; let args = arguments; clearTimeout(timer); //清除定时器 timer = setTimeout(function () { fn.apply(context, args); }, delay); } }

    使用方法:

    import { debounce } from "~/assets/js/tool"; //首先引入debounce //使用方式 debounce(() => { //需要执行函数 console.log('执行函数') }, 1000) //时间可以自己设置
    Processed: 0.013, SQL: 9