From b592850a0af87c6d8f5f51a8cd4aa4902089647c Mon Sep 17 00:00:00 2001 From: linfso Date: Sat, 25 May 2024 22:22:39 +0800 Subject: [PATCH] md --- src/utils/input/debounceModule.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/utils/input/debounceModule.js diff --git a/src/utils/input/debounceModule.js b/src/utils/input/debounceModule.js new file mode 100644 index 00000000..859b76e6 --- /dev/null +++ b/src/utils/input/debounceModule.js @@ -0,0 +1,27 @@ + +// 节流 +export function throttleFun(fn, wait = 500) { + let last, now + return function () { + now = Date.now() + if (last && now - last < wait) { + last = now + } else { + last = now + fn.call(this, ...arguments) + } + } +} + +// 防抖 +export function debounceFun(fn, wait = 500) { + let timer + return function () { + let context = this + let args = arguments + if (timer) clearTimeout(timer) + timer = setTimeout(() => { + fn.apply(context, args) + }, wait) + } +}