Categories
JavaScript Answers

How to use setInterval and clearInterval with JavaScript?

Spread the love

Sometimes, we want to use setInterval and clearInterval with JavaScript.

In this article, we’ll look at how to use setInterval and clearInterval with JavaScript.

How to use setInterval and clearInterval with JavaScript?

To use setInterval and clearInterval with JavaScript, we call clearInterval to clear the repeating timer with the timer ID which is returned by setInterval.

For instance, we write

const handle = setInterval(drawAll, 20);

//...

clearInterval(handle);

to call setInterval with the drawAll function and 20 milliseconds to call drawAll every 20 milliseconds.

And then we call clearInterval with the handle to clear the timer returned by setInterval.

Conclusion

To use setInterval and clearInterval with JavaScript, we call clearInterval to clear the repeating timer with the timer ID which is returned by setInterval.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *