How to call a function repeatedly every 5 seconds in JavaScript ? Last Updated : 09 Aug, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, the setInterval() method allows you to repeatedly execute a function or evaluate an expression at specified intervals. This method is particularly useful for performing periodic tasks like updating a user interface or making repeated API calls.Syntax: setInterval(function, milliseconds, param1, param2, ...)Parameters: This function accepts the following parameters: function: This parameter holds the function name which to be called periodically.milliseconds: This parameter holds the period, in milliseconds, setInterval() calls/executes the function above.param1, param2, ... : Some additional parameters to be passed as input parameters to function.Return Value: This method returns the ID representing the timer set by the method. This ID can be used to clear/unset the timer by calling the clearInterval() method and passing it to this ID as a parameter.Example: Here, the setInterval() method repeatedly evaluates an expression/calls a function. The way to clear/unset the timer set by the setInterval() method is to use the clearInterval() method and pass it the ID/value returned on calling setInterval(). HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>function repeatedly every 5 seconds</title> </head> <body> <p> Click the button to start timer, you will be alerted every 5 seconds until you close the window or press the button to stop timer </p> <button onclick="startTimer()"> Start Timer </button> <button onclick="stopTimer()"> Stop Timer </button> <p id="gfg"></p> <script> let timer; function startTimer() { timer = setInterval(function () { document.getElementById('gfg') .innerHTML = " 5 seconds are up "; }, 5000); } function stopTimer() { document.getElementById('gfg') .innerHTML = " Timer stopped "; clearInterval(timer); } </script> </body> </html> Output: How to call a function repeatedly every 5 seconds in JavaScript ? Comment More infoAdvertise with us Next Article How to call a function repeatedly every 5 seconds in JavaScript ? H htewari Follow Improve Article Tags : JavaScript Web Technologies JQuery JavaScript-Questions Similar Reads How to Delay a Function Call in JavaScript ? Delaying a JavaScript function call involves executing a function after a certain amount of time has passed. This is commonly used in scenarios where you want to postpone the execution of a function, such as in animations, event handling, or asynchronous operations. Below are the methods to delay a 2 min read How to Delay a JavaScript Function Call using JavaScript ? Delaying the execution of a JavaScript function is a powerful technique often achieved using methods like setTimeout(). This approach is commonly used in over 60% of modern web applications to manage animations, control user interactions, or handle asynchronous behavior smoothly. Whether it's for de 2 min read How to run a function in a separate thread by using a Web Worker in JavaScript ? JavaScript is a popular lightweight, interpreted compiled client-side scripting language. Most Web Applications use JavaScript on the client side. By providing JavaScript with a runtime environment, it can also be used on the server-side (Node.js). In this article, we will cover how to run a functio 3 min read How to stop setInterval Call in JavaScript ? 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 repeti 2 min read How to create an Asynchronous function in Javascript? JavaScript is a single-threaded and synchronous language. The code is executed in order one at a time. But Javascript may appear to be asynchronous in some situations. Example: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title 6 min read Like