title: How to unsubscribe from setinterval only when input updated
body: <p>I need some help with set interval.</p>
  <p>I have 2 fields, 1st is an input for insert milleseconds for interval , and the second one is the count itself.</p>
  <p>I need to run counter according to the milleseconds that inserted to the input:</p>
  <pre><code>import &quot;./styles.css&quot;;
  import { useState, useEffect, useRef } from &quot;react&quot;;
  
  export default function App() {
    const [count, setCount] = useState(0);
    const [ms, setMS] = useState(1000);
  
    useEffect(() =&gt; {
      const interval = setInterval(() =&gt; {
        console.log(&quot;Start inteval&quot;);
        setCount(count + 1);
      }, ms);
  
      return () =&gt; {
        console.log(&quot;Clear interval&quot;);
        clearInterval(interval);
      };
    });
  
    return (
      &lt;div className=&quot;App&quot;&gt;
        &lt;input value={ms} onChange={(e) =&gt; setMS(e.target.value)}&gt;&lt;/input&gt;
        &lt;h1&gt;{count}&lt;/h1&gt;
      &lt;/div&gt;
    );
  }
  </code></pre>
  <p>For now it's working, but the problem is in my return (unsubscribe method).</p>
  <p>I need to clear the interval only when the input changed and I need to update the ms for interval.</p>
  <p>Some one for best solution pls?</p>