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 GeeksforGeeks 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 Like