JavaScript setTimeout() Method Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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(function, delay); Parameters:function: The function or code snippet to be executed after the specified delay.delay: The time, in milliseconds, to wait before executing the function.Return Value:Returns a Number which is the id of the timer. Use this id with clearTimeout(id) to cancel the timer. Example 1: Here, the greet function will be executed after a delay of 2000 milliseconds (2 seconds). JavaScript function greet() { console.log("Hello, world!"); } // Call the greet function after // 2000 milliseconds (2 seconds) setTimeout(greet, 2000); Output: (Will be printed after 2 sec or 2000ms) Hello, world! Example 2: Below is the example of popping an up alert, 2 seconds(2000ms) after the user presses the click me button. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport"> <title>Document</title> </head> <body> <button onclick="setTimeout(gfg, 2000);"> Press me </button> <script> function gfg() { alert('Welcome to GeeksforGeeks'); } </script> </body> </html> Output: Note: We can stop the execution of the setTimeout() function by using a method called as clearTimeout() or by closing the window.Example 3: Here, we are using a setTimeout() function and stop its execution using the clearTimeout() function before the execution of the setTimeout(). HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport"> <title>Document</title> </head> <body> <p>Press the stop button before the alert is shown</p> <button onclick="val = setTimeout(gfg, 2000);"> Press me </button> <button onclick="clearTimeout(val);"> Stop Execution</button> <script> function gfg() { alert('Welcome to GeeksforGeeks'); } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article JavaScript setTimeout() Method A amanv09 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Methods 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() & 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 Throttling Throttling is a technique used to limit the number of times a function can be executed in a given time frame. Itâs extremely useful when dealing with performance-heavy operations, such as resizing the window or scrolling events, where repeated triggers can lead to performance issues.JavaScriptfuncti 3 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 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 How to Make JavaScript Sleep or Wait? In JavaScript, there is no built-in sleep function like in some other programming languages. However, you can create a delay or pause in code execution by using asynchronous methods.We can use setTimeout() or Promise along with async/await to achieve this functionality for more readable code.1. Usin 2 min read Node.js http.server.setTimeout() Method The http.server.setTimeout() is an inbuilt application programming interface of the class Server within HTTP module which is used to set the time-out value for the socket. Syntax: server.setTimeout([msecs][, callback]) Parameters: This method takes the socket time-out value in a millisecond. Return 3 min read How to Set Time Delay in JavaScript? Delaying the execution of code is a fundamental technique that is commonly used in JavaScript for tasks like animations, API polling, or managing time intervals between actions. JavaScript provides several built-in methods to set time delays: setTimeout() and setInterval(). We can set time delay in 2 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 jQuery setTimeout In jQuery, the setTimeout function is not a built-in jQuery function, it's a standard JavaScript function. You can use it in jQuery to introduce delays in your code. Make sure to include the jQuery library in your HTML file before using jQuery code: <script src= "https://cdnjs.cloudflare.com/ajax 2 min read Like