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.
Asked: 2023-04-03 11:00:00 +0000
Seen: 11 times
Last updated: Apr 18 '22