Coder Social home page Coder Social logo

lobsterbandit / use-countdown-timer Goto Github PK

View Code? Open in Web Editor NEW
33.0 33.0 5.0 5.91 MB

React hook exposing a countdown timer with optional expiration reset callbacks

Home Page: https://lobsterbandit.github.io/use-countdown-timer/

License: MIT License

HTML 10.16% TypeScript 82.99% JavaScript 6.61% Dockerfile 0.24%
countdown react react-hooks timer typescript

use-countdown-timer's People

Contributors

dependabot[bot] avatar janrembold avatar lobsterbandit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

use-countdown-timer's Issues

Timer doesn't stop when using in tests

Here is my component's code:

const Component = ({onSend}) => {
  const { countdown, start, reset, isRunning } = useCountdownTimer({
    timer: 1000 * 30,
  });

  const handleSubmit = useCallback(
    (values) => {
      onSend(values.phone);
      reset();
      start();
    },
    [start, reset, onSend];
  );
  
  return (
    <Button onClick={handleSubmit} disabled={isRunning}>Submit {countdown / 1000}</Button>
  );
}

and here is my test:

expect(submitButton).toBeDisabled();

act(() => {
  jest.advanceTimersByTime(31000);
});

expect(submitButton).toBeEnabled();

I found out that timer doesn't stop in this case, it shows 0 seconds, then -1 second, -2 seconds and so on...

Am I doing something wrong?

Add Pause method

Pause method should halt the current timer until start() or reset() is called.
Countdown return value should remain the same until

  • start() - Resume countdown timer. Countdown will change on next interval tick.

  • reset() - Clear countdown timer, resetting to initial timer value and running any onReset callbacks.

interval performance issue

when i pause actually what it happens it make isRunning and canStart to false , but i don't see clear interval for this senario
i think my solution can resolve this issue but i think i need to move interval ids in top level so i can access it on next render use Effect.
image

Provide source maps

When using CRA 5, which uses Webpack 5, I'm getting the source-map error:
Failed to parse source map from '/node_modules/use-countdown-timer/src/index.ts

Please generate source maps as a part of the build output. Thanks.

after expire, don't reset to initial value

Hey there, and thanks for the neat hook.

In my use case, I'm displaying a decreasing circular progress bar, based on the countdown. Currently when the countdown expires, the countdown value resets to initial, and causes my progress bar to rewind. I'd like to be able to stay at 0 value and only reset to initial when I actually call reset.

I would argue that this behavior should be the default one... what do you think?

restart problem

i cannot reset() then start() again because when i want to start countdown is 0 and doesnt trigger the start

Should honor autostart when timer is reset?

Apply autostart preference when timer is reset rather than only setting the initial state value.

Provide separate parameter startInitial or something to only set the initial state value.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.