amrlabib / react-timer-hook

React timer hook

Home Page:https://www.npmjs.com/package/react-timer-hook

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Count up from a given timestamp

DuncanLewis opened this issue · comments

Is there any way to count up from a timestamp, i.e useStopwatch with a start timestamp passed to start the count from, or useTimer with a timestamp in the past.

I have tried doing the following, but this immediately expires

const t = new Date();
  t.setSeconds(t.getSeconds() - 600);
  const { hours, minutes, seconds } = useTimer({
    t,
  });

If this isn't available no worries, just wondered if I am missing something obvious. Thanks!

@DuncanLewis useTimer(countdown) should get timestamp in future so if you want 5 minutes timer you will just pass now() + 5 minutes and the way it works is by subtracting passed timestamp from now so basically passing timestamp in past will not work and will immediately expire.

For useStopwatch(count up) it will always start from 0 and count up.

I didn't fully understand what you are trying to achieve, are you trying to use useStopwatch but starting from specific minutes example instead of starting from 0 it should start from 5 minutes ? is that what you are trying to achieve ? please let me

Thanks

Thanks for replying @amrlabib - You have pretty much understand what I'm trying to achieve - a counter show elapsed time, e.g.
00:00:01... 00:00:02.. as we can do with useStopwatch - however I need to do this starting from a timestamp in the past, rather than from 0.

So if we say it will start 1 minute in the past it should work in the following way right ?

-00:00:59
-00:00:58
-00:00:57
-00:00:56
.
.
.
-00:00:02
-00:00:01
00:00:00
00:00:01
00:00:02
00:00:03
00:00:04
.
.
.

This is what you are trying to achieve @DuncanLewis ?

@DuncanLewis Please let me know if this is what you are trying to do to either implement it or close the issue. Thanks