JavaScript setInterval() Method Last Updated : 03 Jun, 2024 Comments Improve Suggest changes Like Article Like Report The setInterval() method calls a function at specified intervals (in milliseconds). It continues calling the function until clearInterval() is called or the window is closed. This method is useful for tasks that need periodic execution, like updating animations or refreshing data. Important Note: setTimeout() is for executing a function once after a specified delay.setInterval() is for executing a function repeatedly at specified intervals until explicitly cleared.Syntax:setInterval(function, delay);function: The function to be executed at each interval.delay: The time, in milliseconds, between each execution of the function.Return Value:Returns a Number which is basically the id of the timer. Example 1: Here, myFunction will be executed every second (1000 milliseconds). JavaScript function myFunction() { console.log("Executing at regular intervals!"); } // Call myFunction every 1000 milliseconds (1 second) setInterval(myFunction, 1000); Output: (Will be printed after 1 sec or 1000ms) Executing at regular intervals!Example 2: Here, we are using setInterval() method. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport"> <title>Document</title> </head> <body> <p id="GFG"></p> <script> let myVar = setInterval(myTimer, 1000); function myTimer() { document.getElementById("GFG") .innerHTML += "<p>Hi</p>"; } </script> </body> </html> Output: After every second a new “hi” message will be displayed. Note: Since the setInterval() method executes the function infinitely hence there is a method called as clearInterval() to stop the execution of the setInterval(). Example 3: In this example, we will first execute a setInterval() function and then stop its execution by using the clearInterval() function. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport"> <title>Document</title> </head> <body> <p id="GFG"></p> <button onclick="clearInterval(myVar)"> Stop Execution</button> <script> let myVar = setInterval(myTimer, 1000); function myTimer() { document.getElementById("GFG") .innerHTML += "<p>Hi</p>"; } </script> </body> </html> Output: When the stop button is clicked the execution is stopped. Comment More infoAdvertise with us Next Article JavaScript setInterval() Method A amanv09 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Methods Similar Reads JavaScript Date setMinutes() Method JavaScript date.setMinutes() method is used to set minutes into a Date object which is created using the Date() constructor. Syntax: DateObj.setMinutes(Minutes_Value) Parameter: This method accepts a single parameter as mentioned above and described below: minutes_Value: This parameter holds the val 3 min read JavaScript setTimeout() Method JavaScript setTimeout() method allows you to schedule the execution of a function or the evaluation of a code after a specified delay. The setTimeout() method calls a function after several milliseconds. setTimeout() is for executing a function once after a specified delay. Syntax:setTimeout(functio 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 Date setUTCMinutes() Method The date.setUTCMinutes() method is used to set minutes according to universal time into a date object which is created using the Date() constructor. Syntax: DateObj.setUTCMinutes(Minutes_Value); Parameter: This method accepts a single parameter as mentioned above and described below: minutes_Value: 4 min read JavaScript Set Date Methods There are various methods to set the date in JavaScript. The data values can be set like years, months, days, hours, minutes, seconds, and milliseconds for a Date Object. Method: setDate(): It is used to set the day as a number (1-31).setFullYear(): It is used to set the year (optionally month and d 2 min read JavaScript Date setSeconds() Method The date.setSeconds() method is used to set seconds into a Date object which is created using Date() constructor. Syntax: DateObj.setSeconds(seconds_Value) Parameter: This method accepts a single parameter as mentioned above and described below: seconds_Value: This parameter holds the value of secon 4 min read JavaScript Date setMilliseconds() Method The date.setMilliseconds() method is used to set milliseconds into a date object which is created using the date() constructor. Syntax: dateObj.setMilliseconds(milliseconds_Value); Parameter: This method accepts a single parameter as mentioned above and described below: milliseconds_Value: This para 4 min read JavaScript Timer A timer has been shown that shows the countdown, and its color/message gets changed after every specific period. Prerequisites: GetMinutes()GetSeconds()SetInterval() MethodSyntax: setTimeout(function, milliseconds, parameter1, ...);Parameter:AttributeDescriptionfunctionIt is the function that will b 2 min read JavaScript Date setUTCMilliseconds() Method The date.setUTCMilliseconds() method is used to set milliseconds according to universal time into a date object which is created using Date() constructor. Syntax: DateObj.setUTCMilliseconds(milliseconds_Value); Parameter: This method accepts a single parameter as mentioned above and described below: 4 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 Like