JavaScript setTimeout() & setInterval() Method Last Updated : 13 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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. JavaScript setTimeout(gfg1, 2000); function gfg1() { console.log("gfg1"); } function gfg() { console.log("gfg"); } setInterval(gfg, 1000); Output:gfg gfg1 gfg ......Key points:gfg1 will be called once as setTimeout is set for 2 secondsgfg function will called each second as it passed through the setInterval. JavaScript setTimeout() MethodThe setTimeout() Method executes a function, after waiting a specified number of milliseconds. JavaScript setTimeout(gfg1, 2000); function gfg1() { console.log("gfg1"); } Output:gfg1JavaScript setInterval() MethodThe setInterval() method repeats a given function at every given time interval. JavaScript function gfg() { console.log("gfg"); } setInterval(gfg, 1000); Output: After every second a new "gfg" message will be displayed.Interesting FactsAsynchronous Execution: Both methods are asynchronous, meaning the browser doesn’t block other code execution while waiting for the timer.Return Value: Both methods return a unique identifier (ID), which can be used with clearTimeout() or clearInterval() to stop the scheduled task.Timer Accuracy: Timers are not perfectly precise; delays can vary due to browser limitations and other queued tasks.Infinite Intervals: Using setInterval() without a clearInterval() call can lead to infinite loops, potentially causing performance issues.Nested Timers: A setTimeout() can mimic a setInterval() by recursively calling itself after each execution.Minimum Delay: The minimum delay for setTimeout() or setInterval() is 4 milliseconds, though it may increase in inactive browser tabs for performance reasons.Timer in Node.js: Both methods are also available in Node.js with similar behavior but are part of the global object.We 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. Comment More infoAdvertise with us Next Article JavaScript setTimeout() & setInterval() Method G geeksforgeeks user Follow Improve Article Tags : Misc JavaScript Web Technologies javascript-functions Practice Tags : Misc Similar Reads JavaScript setInterval() Method 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: se 2 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 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 Program to Pass Parameter to a setTimeout() Method In this article, we will discuss how to pass parameters to the setTimeout() method in JavaScript. The setTimeout() method is used to delay the execution of a piece of code. This method executes a function, after waiting a specified number of milliseconds. We have different approaches to passing para 3 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 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 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 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 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 Change the Time Interval of setinterval() Method at RunTime using JavaScript ? In this article, we are going to learn how to change the setInterval() time after running one element. The setInterval() method reads the timing once and invokes the function at regular intervals. There are two methods to solve this problem which are discussed below:Table of ContentUsing clearInterv 2 min read Like