I am creating a game with react and i want to add a chronometre feature to track time but i tried to use settimeout and it failed bcs every render the settimeout recall and i tried setinterval but failed also can anyone suggest me a way to to this the game is a tenzies game
If you are using a functional component then you are going to have to set an interval constant in the useEffect() hook:
useEffect(() => {
const interval = setInterval(() => {
//code inside here will run every second
}, 1000); //change the 1000 to however many miliseconds you want between execution
return () => clearInterval(interval);
}, []);
This code will run whatever is in the setInterval body every second, but this amount can be changed (change the '1000'). The interval will then be cleared. Note that this interval will stop counting when the component is unmounted