Giter Site home page Giter Site logo

countdown-lib's Introduction

Countdown Library

TypeScript Vite GitHub NPM

Report issue or request a feature · Make a pull request


Table of Contents


Features

  • Written in TypeScript.
  • Can be used with any JS library or framework.
  • Uses (setInterval).
  • Easy to use callback method.
  • Return values include days, hours, minutes and seconds.
  • Optional methods of alternative implementations.

Installing

Using npm:

$ npm install countdown-lib

Examples

Vanilla TypeScript

import { Countdown, RemainingTimeType } from "countdown-lib";

// Any JavaScript supported date format can be used as long as it is passed as a Date object.
const countdown = new Countdown(new Date(2023, 0, 1, 0, 0, 0));

countdown.startCounter((remainder: RemainingTimeType, isComplete: boolean) => {
  // isComplete will return false when all values are zero.
  if (!isComplete) {
    // Remainder should look something like this:
    // { days: 45, hours: 5, minutes: 32, seconds: 12 }
    console.log(remainder);
  }
});

React 16.8+

// These react imports may vary depending on your build tools.
import { react, FC, useState, useEffect } from "react";
import { Countdown, RemainingTimeType } from "countdown-lib";

export const CountdownExample: FC = () => {
  const [remainingTime, setRemainingTime] = useState<RemainingTimeType>({
    days: 0,
    hours: 0,
    minutes: 0,
    seconds: 0,
  });
  const [countdownComplete, setCountdownComplete] = useState<boolean>(false);
  const countdown = new Countdown(new Date(2023, 0, 1, 0, 0, 0));

  useEffect(() => {
    if (isComplete) return;

    countdown.startCounter(
      (remainder: TimeRemainingType, isComplete: boolean) => {
        setRemainingTime(remainder);
        if (isComplete) {
          setIsComplete(isComplete);
        }
      }
    );
  }, [remainingTime, setRemainingTime]);

  return (
    <div>{`${remainingTime.days} ${remainingTime.hours} ${remainingTime.minutes} ${remainingTime.seconds}`}</div>
  );
};

Countdown-lib API

Countdown()

// Breaking change as of version 2.1.0, constructor must be initialised with a Date object.
new Countdown(endDate: Date)

countdown.startCounter(callback)

// Starts a instance of setInterval that executes a callback every 1000ms with arguments
// for remaining time and a truthy value when countdown is complete.
countdown.startCounter(callback(remainder: TimeRemainingType, isComplete: boolean) => void): void;

countdown.stopCounter()

// Clears setInterval
countdown.stopCounter(): void

countdown.getRemainingTime()

// Optional method for retrieving remaining time outside of the .startCounter() callback.
countdown.getRemainingTime(): RemainingTimeType

countdown.addLeadingZero()

// Add leading zeroes to values < 10, ie. 01, 02, 03.
countdown.addLeadingZero(value: number): string

Exposed Types

RemainingTimeType

type RemainingTimeType = {
  [key: string]: number;
  days: number;
  hours: number;
  minutes: number;
  seconds: number;
};

(back to top)

countdown-lib's People

Contributors

fmrogers avatar

Watchers

 avatar  avatar

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.