How to Clear Interval in JavaScript

[2821 views]




setInterval() function is used in Javascript to perform some operation after specified intervals. But sometimes there is need when we need to clear the interval in Javascript.

To clear the interval, we can use clearInterval() function of Javascript which accepts interval object as its first argument.

Javascript Clear Interval Example:

var counter = 0; var intervalVar = setInterval(myCounter, 1000, counter); function myCounter() { counter += 1 if (counter == 3) { clearInterval(intervalVar); console.log('interval stopped!'); } }

In the above code, we are incrementing the counter by 1, every time an interval runs and when the counter value is 3 (i.e any condition is met), we call clearInterval and pass it the intervalVar object.

                 






Comments










Search Anything:

Sponsored Deals ends in



Technical Quizzes Specially For You:

Search Tags

    Javascript Clear Interval Code Example