![]() |
VOOZH | about |
In JavaScript there are many inbuilt functions clearTimeout() and clearInterval() methods are some of them. when we use setTimeout() and setInterval() in any JavaScript program then it must clear the time used in those functions so we use the clearTimeout() and clearInterval() methods.
setTimeout() and setInterval()
The clearTimeout() function in javascript clears the timeout which has been set by the setTimeout()function before that.
clearTimeout(name_of_setTimeout);Example: In this example, we will write a function to clear the timeout set by the setTimeout() function using the clearTimeout() function.
Output: The GeeksForGeeks button color changes after 2 seconds just one time. Click on Stop 2 seconds after clicking the GeeksForGeeks button to clear Timeout.
👁 ImageThe clearInterval() function in javascript clears the interval which has been set by the setInterval() function before that.
clearInterval(nameOfInterval);Example: In this example, we will write a function to clear the interval set by the setInterval() function using the clearInterval() function.
Output: In this example, the GeeksForGeeks color changes and stays the same every second, after that it changes again. Click on Stop to clear the interval.
👁 ImageWe have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.