参考如下demo,可以实现点击几次触发某个方法,并且已经封装成组件,使用时传入方法和次数就行:import { promptAction } from '@kit.ArkUI' function throttle(func: () => void, time: number = 5) { let initalTime = time return () => { if (time > 1) { time-- promptAction.showToast({message:'剩余'+time.toString()+'次'}) console.log(time.toString()) } else { time = initalTime func() } }; } function sayThrottle() { promptAction.showToast({ message: `已完成` }) } @Component export struct MultiClick { build() { Column() { Button('节流点击') .onClick( throttle(() => { sayThrottle() }, 7)) } } }
参考如下demo,可以实现点击几次触发某个方法,并且已经封装成组件,使用时传入方法和次数就行: