From f26f29d84e0a68831a6af14dab3eec5500496d2e Mon Sep 17 00:00:00 2001 From: spring <2396852758@qq.com> Date: 星期三, 28 五月 2025 16:48:52 +0800 Subject: [PATCH] 初始化项目 --- uni_modules/wu-ui-tools/libs/function/debounce.js | 29 +++++++++++++++++++++++++++++ 1 files changed, 29 insertions(+), 0 deletions(-) diff --git a/uni_modules/wu-ui-tools/libs/function/debounce.js b/uni_modules/wu-ui-tools/libs/function/debounce.js new file mode 100644 index 0000000..ad3996b --- /dev/null +++ b/uni_modules/wu-ui-tools/libs/function/debounce.js @@ -0,0 +1,29 @@ +let timeout = null + +/** + * 闃叉姈鍘熺悊锛氫竴瀹氭椂闂村唴锛屽彧鏈夋渶鍚庝竴娆℃搷浣滐紝鍐嶈繃wait姣鍚庢墠鎵ц鍑芥暟 + * + * @param {Function} func 瑕佹墽琛岀殑鍥炶皟鍑芥暟 + * @param {Number} wait 寤舵椂鐨勬椂闂� + * @param {Boolean} immediate 鏄惁绔嬪嵆鎵ц + * @return null + */ +function debounce(func, wait = 500, immediate = false) { + // 娓呴櫎瀹氭椂鍣� + if (timeout !== null) clearTimeout(timeout) + // 绔嬪嵆鎵ц锛屾绫绘儏鍐典竴鑸敤涓嶅埌 + if (immediate) { + const callNow = !timeout + timeout = setTimeout(() => { + timeout = null + }, wait) + if (callNow) typeof func === 'function' && func() + } else { + // 璁剧疆瀹氭椂鍣紝褰撴渶鍚庝竴娆℃搷浣滃悗锛宼imeout涓嶄細鍐嶈娓呴櫎锛屾墍浠ュ湪寤舵椂wait姣鍚庢墽琛宖unc鍥炶皟鏂规硶 + timeout = setTimeout(() => { + typeof func === 'function' && func() + }, wait) + } +} + +export default debounce -- Gitblit v1.9.3