Use the clearInterval() method to stop JavaScript setInterval. The setInterval() returns an interval ID, which you can pass to clearInterval():
intervalId= setInterval(function, milliseconds);Then you can stop the execution by calling clearInterval():
clearInterval(intervalId); JavaScript setInterval stop
Simple example code.
<!DOCTYPE html>
<html>
<body>
  
  <p id="demo"></p>
  <button onclick="myStop()">Stop the time</button>
  <script>
    const myInterval = setInterval(myTimer, 1000);
    function myTimer() {
      const date = new Date();
      document.getElementById("demo").innerHTML = date.toLocaleTimeString();
    }
    function myStop() {
      clearInterval(myInterval);
    }
  </script>
</script>
</body>
</html> Output:

You can set a new variable and have it incremented by ++ (count up one) every time it runs, then I use a conditional statement to end it:
var intervalId = null;
var varCounter = 0;
var varName = function(){
     if(varCounter <= 10) {
          varCounter++;
          /* your code goes here */
     } else {
          clearInterval(intervalId);
     }
};
$(document).ready(function(){
     intervalId = setInterval(varName, 10000);
});Do comment if you have any doubts or suggestions on this JS set Interval topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version