minwork / use-long-press

React hook for detecting click (or tap) and hold event
MIT License
122 stars 12 forks source link

`threshold` is only observed up to 85% of given value. How to get current value of timer? #33

Closed pawelkrystkiewicz closed 2 years ago

pawelkrystkiewicz commented 2 years ago

threshold bug

Hi, I found out that passed threshold value is not being observed correctly by the hook. It triggers end at about 85% time elapsed. I set my button timer to 2000ms and it got triggered around 1710 to 1750 (it is not constant)

In my code I needed to add this hack:

const BUTTON_TIMER = 2000 //ms

...

{   ...
    threshold: BUTTON_TIMER / 0.85,
    ...
}

How to get time left?

Besides that it would be ideal if I could get my hand on value of time left from the hook. Is it possible? My use case is that I'm showing how much time left by progress bar around button

image

minwork commented 2 years ago

Hey, this behaviour is specific to how setTimeout is working (browser don't guarantee that setTimeout for 2000ms will actually trigger callback after exactly 2000ms). If you want self correcting timer take a look at https://github.com/minwork/react-interval-hook