JavaScript setTimeout() & setInterval() Method Last Updated : 11 Jul, 2025 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. SetTimeout,SetInterval and clearInterval clearTimeout Visit Course Comment More info GeeksforGeeks Improve Article Tags : Misc JavaScript Web Technologies javascript-functions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings6 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like