Skip to content

Instantly share code, notes, and snippets.

@Alzemiro
Created October 24, 2023 19:41
Show Gist options
  • Save Alzemiro/33e01efd0ab1cd40115c0f1f4b8af18f to your computer and use it in GitHub Desktop.
Save Alzemiro/33e01efd0ab1cd40115c0f1f4b8af18f to your computer and use it in GitHub Desktop.
To Sther
import React, { useState, useEffect } from 'react';
export function App(props) {
const [time, setTime] = useState(25000);
const [start, setStart] = useState(false);
useEffect(() => {
const timer = setInterval(() => {
if (start && time > 0) {
setTime(time - 1);
}
}, 1000);
return () => clearInterval(timer);
}, [start, time]);
function startStopClock() {
setStart(!start);
}
return (
<div className='App'>
<h1>{time.toString()}</h1>
<button onClick={startStopClock}>{`${!start ? 'Start' : 'Stop'}`}</button>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment