Skip to content

Instantly share code, notes, and snippets.

@IsraelViPe
Last active February 5, 2023 21:03
Show Gist options
  • Select an option

  • Save IsraelViPe/bd42ab2509231d88463bfa75e04dcbb2 to your computer and use it in GitHub Desktop.

Select an option

Save IsraelViPe/bd42ab2509231d88463bfa75e04dcbb2 to your computer and use it in GitHub Desktop.
ContDown
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>
)
}
@IsraelViPe
Copy link
Author

Um simples contDown em React que eu tava estudando.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment