How to stop setInterval Call in JavaScript ? Last Updated : 04 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In JavaScript, the setInterval() function is used to repeatedly execute a specified function at a fixed interval. However, there may be scenarios where we need to stop the execution of setInterval() calls dynamically. Stopping a setInterval() call in JavaScript is essential to prevent ongoing repetitive tasks and manage resource usage efficiently. Using clearInterval methodIn this approach, we are using clearInterval the interval ID returned setInterval to directly stop the repetitive task execution at any point in your code. Example 1: Implementation to show how to stop setInterval call in JavaScript. JavaScript let intervalId = setInterval(() => { console.log('Executing repetitive task...'); }, 1000); // Stop the interval after 5 seconds setTimeout(() => { clearInterval(intervalId); console.log('Interval stopped after 5 seconds.'); }, 5000); Output: Example 2: Implementation to show how to stop setInterval call in JavaScript using conditional check. JavaScript let counter = 0; let intervalId = setInterval(() => { counter++; console.log('Counter:', counter); if (counter >= 5) { clearInterval(intervalId); console.log('Interval stopped after 5 iterations.'); } }, 1000); Output: Example 3: Implementation to show how to stop setInterval call in JavaScript using . JavaScript let isRunning = true; let intervalId = setInterval(() => { if (isRunning) { console.log('Executing repetitive task...'); } else { clearInterval(intervalId); console.log('Interval stopped using flag.'); } }, 1000); // Stop the interval after 5 seconds using the flag setTimeout(() => { isRunning = false; }, 5000); Output: Comment More infoAdvertise with us Next Article JavaScript setTimeout() & setInterval() Method M mguru4c05q Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Set Time Delay in JavaScript? Delaying the execution of code is a fundamental technique that is commonly used in JavaScript for tasks like animations, API polling, or managing time intervals between actions. JavaScript provides several built-in methods to set time delays: setTimeout() and setInterval(). We can set time delay in 2 min read JavaScript setTimeout() & setInterval() Method JavaScript SetTimeout and SetInterval are the only native function in JavaScript that is used to run code asynchronously, it means allowing the function to be executed immediately, there is no need to wait for the current execution completion, it will be for further execution.JavaScriptsetTimeout(gf 2 min read JavaScript setTimeout() & setInterval() Method JavaScript SetTimeout and SetInterval are the only native function in JavaScript that is used to run code asynchronously, it means allowing the function to be executed immediately, there is no need to wait for the current execution completion, it will be for further execution.JavaScriptsetTimeout(gf 2 min read Difference Between setTimeout & setInterval in JavaScript JavaScript has both setTimeout and setInterval functions that are used for executing code after a certain delay. However, they differ in how they handle the timing of execution. Understanding their differences is crucial for effectively managing asynchronous operations in our code which is explained 2 min read How to Detect Idle Time in JavaScript ? The idle time is the time that the user doesn't interact with a web page. This interaction can be either moving the mouse, clicking on the page, or using the keyboard. This time can be detected to execute certain events that may need to occur after a certain period of idle time. \ Method 1: Using Ja 4 min read How to Detect Idle Time in JavaScript ? The idle time is the time that the user doesn't interact with a web page. This interaction can be either moving the mouse, clicking on the page, or using the keyboard. This time can be detected to execute certain events that may need to occur after a certain period of idle time. \ Method 1: Using Ja 4 min read Like