Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

countdown in react js

import React from 'react'
import { useState, useEffect } from 'react';

const Timer = (props:any) => {
    const {initialMinute = 0,initialSeconds = 0} = props;
    const [ minutes, setMinutes ] = useState(initialMinute);
    const [seconds, setSeconds ] =  useState(initialSeconds);
    useEffect(()=>{
    let myInterval = setInterval(() => {
            if (seconds > 0) {
                setSeconds(seconds - 1);
            }
            if (seconds === 0) {
                if (minutes === 0) {
                    clearInterval(myInterval)
                } else {
                    setMinutes(minutes - 1);
                    setSeconds(59);
                }
            } 
        }, 1000)
        return ()=> {
            clearInterval(myInterval);
          };
    });

    return (
        <div>
        { minutes === 0 && seconds === 0
            ? null
            : <h1> {minutes}:{seconds < 10 ?  `0${seconds}` : seconds}</h1> 
        }
        </div>
    )
}

export default Timer;
Comment

react countdown

yarn add react-countdown
Comment

PREVIOUS NEXT
Code Example
Javascript :: Get the value of text input field 
Javascript :: rounding number to x decimals javascript 
Javascript :: regex optional whitespace characters 
Javascript :: calculate width of text javascript 
Javascript :: how to delete file from firebase storage on web 
Javascript :: css canvas set aspect ratio 
Javascript :: How to get the Class Name of an Object in JavaScript 
Javascript :: hmget in redis 
Javascript :: sequelize init connection set up nodejs node 
Javascript :: js map array to dictionary 
Javascript :: $(document).ready(function() alert 
Javascript :: why is my deleteOne mongoose middleware not working 
Javascript :: js float to percentage 
Javascript :: javascript determine if string is valid url 
Javascript :: show hidden element javascript 
Javascript :: using multiparty with node js express 
Javascript :: how to add seconds to time in js 
Javascript :: mv multiple directories 
Javascript :: js text to html 
Javascript :: convert string to set in js 
Javascript :: map index 
Javascript :: get version from package.json 
Javascript :: javascript redirect to url with parameters 
Javascript :: debounce js 
Javascript :: react native npm run start port 
Javascript :: wordpress javascript localization 
Javascript :: jquery .click function call 
Javascript :: detect livewire is loading in javascript 
Javascript :: hashing in node js 
Javascript :: get the most recent records in mongoose 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =