diff --git a/src/main.js b/src/main.js index 7618839..693feaf 100644 --- a/src/main.js +++ b/src/main.js @@ -27,6 +27,7 @@ import { selectDictLabels, handleTree, indexContinuation, + debounce, } from '@/utils/ruoyi' // 分页组件 import Pagination from '@/components/Pagination' @@ -65,6 +66,7 @@ Vue.prototype.downloadJson = downloadJson Vue.prototype.handleTree = handleTree Vue.prototype.globalUrl = global_ Vue.prototype.indexContinuation = indexContinuation +Vue.prototype.debounce = debounce // 全局组件挂载 Vue.component('DictTag', DictTag) diff --git a/src/utils/ruoyi.js b/src/utils/ruoyi.js index 368afff..ab682e7 100644 --- a/src/utils/ruoyi.js +++ b/src/utils/ruoyi.js @@ -250,3 +250,18 @@ export function blobValidate(data) { export function indexContinuation(num, size) { return (num - 1) * size + 1 } + +export function debounce(fn, delay) { + let timer = null + return (...args) => { + // 清除之前的定时器 + if (timer) { + clearTimeout(timer) + } + + // 设置新的定时器,延迟执行函数 + timer = setTimeout(() => { + fn.apply(...args) // 使用 `apply` 确保 `this` 指向 Vue 实例 + }, delay) + } +}