How to setinterval and clearinterval in javascript

setinterval and clearinterval in javascript

In this article we will learn How we can setinterval and clearinterval in javascript, this is very simple process in javascript . you can leverage the setInterval() function, which allows you to execute a specified function repeatedly at defined intervals. it is use to create different type of animation. Or if we are not using sockets, the it is also user to fetch data from server priodically by hitting the API call with some time interval.

Understanding setInterval()

The setInterval() method needs two main parameters: a function to execute and the time interval in milliseconds. For instance, if you want to show a message every three seconds, you can use the following code:

In the above code the console message will trigger every after 3sec (3000 milliseconds), but the issue in above code is it will exicute infinite times, until we stop that exicution from the code itself, and to stop time interval we use clearinterval() method. so in above code we use setInterval method in JavaScript to call a function at regular intervals.

To stop the execution of the function set by setInterval(), you need to call clearInterval() with the identifier returned by setInterval(). Here’s how you can implement this:

In the above by using Function Expressions we have cached animate and on page load it will be executed and after 1sec we will be able to see console message This will log every second, but it will be a repeating step to infinity after one second, so to stop this we will use clearinterval()

How to use clearinterval()

In above code we are clearing the recuring interval with clearInterval method after 5 seconds, Now lets go though one more example if we want to clear interval after certain counts

How to stop setInterval in JavaScript after a certain number of executions

The script initializes a counter variable count to 0 and sets a maxCount limit of 5. It uses setInterval to execute a function every 1000 milliseconds (1 second). Within the function, count is incremented and a message is logged to the console. When count reaches maxCount (5), clearInterval is called to stop the interval timer. A final message is logged to indicate that the interval has been stopped. This ensures that the function logs a message only 5 times, then ceases execution.

Now lets go thourgh a real time web ui marketing senario , where we need to hit API call to fetch the updated data after every 10 second

how to use setInterval in JavaScript to fetch data from a server at regular intervals

In above code we have created two element data-container on which we will be able to see the updated data after every 10second, and stop-button is the element on which once clicked it will clear the time interval and stop the api calls. Now below is the javascript code to execute the api calls every after 10 sec.

The provided JavaScript code is designed to fetch and display data from an API at regular intervals on a webpage. It initializes an interval with setInterval to call fetchData() every 10 seconds, ensuring that the UI is updated with the latest data from the API. If the API call is successful, the data is displayed in a div with the ID data-container. If an error occurs, an error message is shown instead. The startFetchingData function triggers the data fetching process immediately upon page load. Additionally, a stopFetchingData function stops the interval and updates the UI when a button with the ID stop-button is clicked.

Further reading

  • React UseEffect | simplifyScript

    Understanding the useEffect hook in React

    In this article, we will learn about another React hook called useEffect(), through this article you will be able to understand what is react useEffect Hook and how to use useEffect() hook in react, this is most communally used hook after useState() hook, we mostly use this hook in every component on react now first […]
  • React useState function

    How to Use useState Hook in React Application

     In a React application, normal vanilla JavaScript may not work for some concepts. For example, if I want to toggle a class on a button click or add and remove classes, I might need to show a popup on the website. This can be easily achieved in vanilla JavaScript using the element.classList.toggle(“newClass”) method, allowing us to add […]
  • javascript DOm manipulation - simplifyscript

    What is DOM manipulation in javascript 

    In this article, we will explore JavaScript DOM manipulation, a key feature of JavaScript that allows developers to modify the Document Object Model (DOM) of a webpage. DOM manipulation enables you to dynamically alter the content and structure of HTML documents. To grasp this concept thoroughly, we’ll start by defining what the DOM is and […]