Skip to content

Instantly share code, notes, and snippets.

@wbarcovsky
Created August 31, 2022 16:09
Show Gist options
  • Select an option

  • Save wbarcovsky/37aa0f5d8d4041b20f69178eb58de665 to your computer and use it in GitHub Desktop.

Select an option

Save wbarcovsky/37aa0f5d8d4041b20f69178eb58de665 to your computer and use it in GitHub Desktop.
export const Timer = (props) => {
const [counter, setCounter] = useState(15);
const text = props.text.toUpperCase();
const handler = React.useRef<any>();
useEffect(() => {
handler.current = setInterval(() => {
setCounter((prev) => prev - 1);
}, 1000);
return () => clearInterval(handler.current);
}, []);
useEffect(() => {
if (counter === 0) {
clearInterval(handler.current);
}
});
return (
<div className="timer">
<div className="counter" style={{border: '1px solid black'}}>
<div className="text">{text}</div>
<div className="timer_counter">{counter}</div>
</div>
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment