I want to make a Reverse Countdown Timer in which that has an input field and when I type a number and press the "Enter" key, the countdown starts. but the problem is when I want to stop the previous interval and starts new interval with new number and press Enter key, it runs parallel with previous one.
I'm pretty new to react, can somebody give me some advice to fix the problem ?
import React, {useState} from "react";
const App = () => {
const [ctime, setctime]=useState();
const enterdown=(value)=>{
clearInterval(interval);
setctime(value);
var interval = setInterval(()=>{
value--;
if(value==0){
clearInterval(interval);
}
setctime(value);
},1000)
}
return (
<div className="wrapper">
<div id="whole-center">
<h1>
Reverse countdown for<input id="timeCount" onKeyDown={(e)=>{e.key == "Enter" ?(enterdown(e.target.value)) :null}} /> sec.
</h1>
</div>
<div id="current-time">{ctime}</div>
</div>
)
}
export default App;