Ask Your Question
1

How can I halt the setInterval function in JavaScript?

asked 2023-04-03 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-04-18 06:00:00 +0000

qstack gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-04-03 11:00:00 +0000

Seen: 11 times

Last updated: Apr 18 '22