const handleStart = () => { setTime(Date.now()); setNow(Date.now()); clearInterval(intervalRef.current); // Edge case: clearing the previous Intervals, intervalRef.current = setInterval(() => { setTime(Date.now()); }, 10); return () => clearInterval(intervalRef.current); }; Like whenever we click restart button more than one time it doesn't stop when i click on the stop So to overcome this clearing timeInterval before creating a new one would result in more proper implementation
If you like the video, don't forget to comment. Thank you for watching ❤
Thanks please keep making more interview videos like these
Shouldn't we use useRef for storing now as we are not going to rerender the UI for it ?
Awesome Sir
Thanks brother 🙂
Nicely explained 🙂
When u stopped the watch at 2 sec and if I want to let's continue from 2 sec then what to do sir?
In that case do not setTime in handlestart(), just set the default value of Time as Date.now()
Make more videos on these type of interview questions
Sure brother
When i create stopwatch but with different condition so how to should i go i stuck at that point please guide me how i build my logic
Hey you can text me at insta at codingjourneywithakash. Send me the ss of your code
Me learning React
const handleStart = () => {
setTime(Date.now());
setNow(Date.now());
clearInterval(intervalRef.current); // Edge case: clearing the previous Intervals,
intervalRef.current = setInterval(() => {
setTime(Date.now());
}, 10);
return () => clearInterval(intervalRef.current);
};
Like whenever we click restart button more than one time it doesn't stop when i click on the stop
So to overcome this clearing timeInterval before creating a new one would result in more proper implementation