From f6cbbdd7c2f494fa5f4eaa7ecb1da3fa5ced1b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A2=A6=E9=BE=99?= Date: Fri, 8 Aug 2025 07:36:36 +0000 Subject: [PATCH 1/2] update ArkUI/entry/src/main/ets/pages/GestureRateLimiting.ets. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨梦龙 --- .../main/ets/pages/GestureRateLimiting.ets | 52 +++++++++++++------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/ArkUI/entry/src/main/ets/pages/GestureRateLimiting.ets b/ArkUI/entry/src/main/ets/pages/GestureRateLimiting.ets index f5150aa..3e1ea68 100644 --- a/ArkUI/entry/src/main/ets/pages/GestureRateLimiting.ets +++ b/ArkUI/entry/src/main/ets/pages/GestureRateLimiting.ets @@ -18,29 +18,47 @@ */ // [Start throttle] -// 定义限流函数 -function throttle(func: Function, interval: number) { - let lastTime = 0; - return () => { - const nowTime = Date.now(); - const remainTime = interval - (nowTime - lastTime); - if (remainTime <= 0) { - lastTime = nowTime; - func(); +// Debouncing: When a function is triggered multiple times within a certain period, debouncing ensures that the function is ultimately executed only once after a specified delay +export function debounce(func: (event: ClickEvent) => void, delay?: number) { + let timer: number; + return (event: ClickEvent) => { + clearTimeout(timer); + timer = setTimeout(() => { + func(event); + }, delay ? delay : 1000); + }; +} + +// Throttling: Execute only once within the specified time frame +export function throttle(func: (event: ClickEvent) => void, delay?: number) { + let inThrottle: boolean; + return (event: ClickEvent) => { + if (!inThrottle) { + func(event); + inThrottle = true; + setTimeout(() => inThrottle = false, delay ? delay : 1000); } }; } -// [End throttle] -// 对Button的点击事件进行节流,500ms内不能重复触发 @Entry @Component struct Index { + @State num: number = 0 + build() { - // [Start on_click] - Button('防止重复点击').onClick(throttle(() => { - // do something - }, 5000)) - // [End on_click] + Row() { + Column() { + Text(this.num.toString()) + Button("click") + .onClick( + debounce(() => { + this.num++ + }, 500)) + } + .width('100%') + } + .height('100%') } -} \ No newline at end of file +} +// [End throttle] \ No newline at end of file -- Gitee From 2ecd83c1d79c44d190a116f7b76aa0255f164d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A2=A6=E9=BE=99?= Date: Fri, 8 Aug 2025 07:38:43 +0000 Subject: [PATCH 2/2] update ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨梦龙 --- .../src/main/ets/pages/CustomRateLimiting.ets | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets b/ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets index 3e95de6..2936fde 100644 --- a/ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets +++ b/ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets @@ -19,25 +19,33 @@ // [Start gesture_modifier] class MyGesture implements GestureModifier { - // 防重点击间隔时间 interval: number = 500; - // 上次点击时间 - lastTime: number = 0; - func: Function = () => { + func: ((event: GestureEvent) => void) | null = null; + + throttle(func: (event: GestureEvent) => void, delay?: number) { + let inThrottle: boolean = false; + return (event: GestureEvent) => { + if (!inThrottle) { + func(event); + inThrottle = true; + setTimeout(() => { + inThrottle = false; + }, delay ? delay : 1000); + } + }; } applyGesture(event: UIGestureEvent): void { + const throttledFunc = this.throttle((gestureEvent: GestureEvent) => { + if (this.func) { + this.func(gestureEvent); + } + }, this.interval); + event.addGesture( new TapGestureHandler({ count: 1, fingers: 1 }) - .onAction((event: GestureEvent) => { - const nowTime = Date.now(); - const remainTime = this.interval - (nowTime - this.lastTime); - if (remainTime <= 0) { - this.lastTime = nowTime; - this.func(); - } - }) - ) + .onAction(throttledFunc) + ); } } @@ -48,9 +56,10 @@ struct Index { @State modifier: MyGesture = new MyGesture(); onPageShow(): void { - this.modifier.func = () => { + // Note: func now accepts a (event: GestureEvent) => void + this.modifier.func = (event: GestureEvent) => { console.info('---onTap---'); - } + }; } build() { -- Gitee