To stop a setInterval
function in JavaScript, you can use the clearInterval
function, passing it the ID returned by the setInterval
function.
To do this, we write:
// Start the setInterval function and save the ID returned by it
var intervalID = setInterval(myFunction, 1000); // Replace myFunction with your actual function and 1000 with the desired interval in milliseconds
// Stop the setInterval function using clearInterval
clearInterval(intervalID);
In this example, we start the setInterval
function by calling setInterval(myFunction, 1000)
.
Replace myFunction
with the function you want to execute repeatedly and 1000
with the desired interval in milliseconds (in this case, 1000 milliseconds or 1 second).
The setInterval
function returns an ID that identifies the interval timer. We save this ID in a variable called intervalID
.
To stop the interval, we call clearInterval(intervalID)
and pass it the intervalID
variable.
This will stop the execution of the function passed to setInterval
at the specified interval.