Last active
February 5, 2023 21:03
-
-
Save IsraelViPe/bd42ab2509231d88463bfa75e04dcbb2 to your computer and use it in GitHub Desktop.
ContDown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useEffect } from "react"; | |
| import { useState } from "react" | |
| export default function MyCountDown () { | |
| const [fullSeconds, setFullSeconds] = useState(5); | |
| const [start, setStart] = useState(false); | |
| const AMOUNT_SECONDS = 25 * 60 | |
| useEffect(() => { | |
| const myCountDown = setInterval(() => { | |
| if(start && fullSeconds > 0) { | |
| setFullSeconds(fullSec => fullSec - 1) | |
| } | |
| }, 1000) | |
| return () => clearInterval(myCountDown); | |
| }, [start,fullSeconds]) | |
| const handleCountDown = ({target:{name}}) => { | |
| if(name === 'restart') { | |
| setFullSeconds(AMOUNT_SECONDS) | |
| setStart(false); | |
| } else { | |
| setStart(!start) | |
| } | |
| } | |
| const min = Math.trunc(fullSeconds / 60) | |
| const sec = fullSeconds % 60 | |
| return ( | |
| <div> | |
| <span>{String(min).padStart(2, '0')}</span> | |
| <span>-</span> | |
| <span>{String(sec).padStart(2, '0')}</span> | |
| <button | |
| type="button" | |
| onClick={handleCountDown}> | |
| {start? 'stop': 'start'} | |
| </button> | |
| <button | |
| name="restart" | |
| onClick={handleCountDown}> | |
| Reiniciar | |
| </button> | |
| </div> | |
| ) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Um simples contDown em React que eu tava estudando.