Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To stop or halt the setInterval function in JavaScript, you can use the clearInterval method by passing the interval ID as a parameter. Here is an example:

// Start the interval
const intervalID = setInterval(myFunction, 1000);

// Stop the interval after 5 seconds
setTimeout(() => {
  clearInterval(intervalID);
}, 5000);

In this example, the setInterval function is called and assigned to a variable called intervalID. To stop the interval after 5 seconds, we use the setTimeout function to call the clearInterval method with the intervalID as a parameter. This will stop the interval and prevent myFunction from running anymore.