HarmonyOS 如何实现连续点击几次触发某个方法?

如题:HarmonyOS 如何实现连续点击几次触发某个方法?

阅读 502
1 个回答

参考如下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))
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进