Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CountDown组件onEnd存在多次回调 #1934

Open
yeeli opened this issue Feb 4, 2024 · 0 comments
Open

CountDown组件onEnd存在多次回调 #1934

yeeli opened this issue Feb 4, 2024 · 0 comments
Assignees

Comments

@yeeli
Copy link

yeeli commented Feb 4, 2024

NutUI React 包名

@nutui/nutui-react

NutUI React 版本号

2.3.6

平台

weapp, alipay

重现链接

https://github.com/jdf2e/nutui-react/blob/next/src/packages/countdown/countdown.taro.tsx

重现步骤

CountDown组件在倒数结束时调用onEnd会存在调用多次

期望的结果是什么?

在未设置millisecond为true的时候, CountDown组件的onEnd方法在秒数等于0时只被调用一次

实际的结果是什么?

在未设置millisecond为true的时候, CountDown组件的onEnd方法被在秒数等于0时被调用了多次

环境信息

No response

其他补充信息

在源码tick方法中, 由于Date.now()获取毫秒级时间, 在tick方法中秒数已经等于0的时候, 毫秒还是会大于0,remainTime 大于0 会使tick方法会存在多次自我调用, 所以导致我未开启毫秒级渲染时, onEnd被多次调用

  const tick = () => {
    stateRef.current.timer = requestAnimationFrame(() => {
      if (stateRef.current.counting) {
        const currentTime = Date.now() - stateRef.current.diffTime
        const remainTime = Math.max(
          stateRef.current.handleEndTime - currentTime,
          0
        )

        stateRef.current.restTime = remainTime
        setRestTime(remainTime)

        if (!remainTime) {
          stateRef.current.counting = false
          pause()
          onEnd && onEnd()
        }

        if (remainTime > 0) {
          tick()
        }
      }
    })
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants