From 7753f8ae52c3f66e97be8993260993d0871b2847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8B=A4=E6=B0=91?= Date: Mon, 14 Apr 2025 11:15:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E5=B7=A5=E5=85=B7=E7=B1=BB):=20?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=8A=82=E6=B5=81=E9=98=B2=E6=8A=96=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=B9=B6=E5=A2=9E=E5=8A=A0=E9=98=B2=E6=8A=96=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 CertManager 中的 debounceHold 方法替换为 throttleHold 方法 - 修正 ToolsHelper 中的 throttleHold 方法实现 - 新增 ToolsHelper 中的 debounceHold 方法实现 --- src/main/ets/utils/ToolsHelper.ets | 33 ++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/ets/utils/ToolsHelper.ets b/src/main/ets/utils/ToolsHelper.ets index 82f78d9..552abe8 100644 --- a/src/main/ets/utils/ToolsHelper.ets +++ b/src/main/ets/utils/ToolsHelper.ets @@ -351,7 +351,7 @@ export class ToolsHelper { new AlertBean({ title: options.title, msg: options.msg, - cancel: options.cancel??{}, + cancel: options.cancel ?? {}, confirm: options.confirm }, dialogTag)) ui.getPromptAction().openCustomDialog(c, { @@ -546,7 +546,7 @@ export class ToolsHelper { * @param fun * @param wait */ - static debounceHold(fun: Function, wait: number = 1500) { + static throttleHold(fun: Function, wait: number = 1500) { let funcValue1 = ToolsHelper.getUniqueId(fun) let hash = md5_hex(funcValue1) if (ToolsHelper.setTimeOutMap.get(hash)) { @@ -568,6 +568,35 @@ export class ToolsHelper { } + /** + * 防抖函数(调用会立即触发,在wait时间内不再触发) + * @param fun + * @param wait + */ + static debounceHold(fun: Function, wait: number = 1500) { + let funcValue1 = ToolsHelper.getUniqueId(fun) + let hash = md5_hex(funcValue1) + const func = ToolsHelper.setTimeOutMap.get(hash) + if (func) { + if (func.startTime + func.timeoutNumber <= new Date().getTime()) { + ToolsHelper.setTimeOutMap.delete(hash) + } else { + return + } + } + ToolsHelper.setTimeOutMap.set(hash, { + timeoutNumber: wait, + startTime: new Date().getTime(), + }) + // 执行函数调用 + fun() + // 拦截在wait期间的函数再次调用,在超时后,将限制解除 + setTimeout(() => { + ToolsHelper.setTimeOutMap.get(hash) && clearTimeout(ToolsHelper.setTimeOutMap.get(hash)?.timeoutNumber) + ToolsHelper.setTimeOutMap.delete(hash) + }, wait) + } + static toString(arrayBuffer: ArrayBuffer) { let decoder = util.TextDecoder.create('utf-8'); return decoder.decodeToString(new Uint8Array(arrayBuffer))