How to Clear Interval in JavaScript

[1491 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

For any inquiry, Contact us at atechdailyweb@gmail.com




Facing Programming Errors. Get Answers within 24 hours:


Recommendation Based on your Interest




Quiz For You:
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags