The Wayback Machine - https://web.archive.org/web/20240805202803/https://www.geeksforgeeks.org/how-to-delay-a-javascript-function-call-using-javascript/
Open In App

How to Delay a JavaScript Function Call using JavaScript ?

Last Updated : 12 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

When we want to call a function after a described amount of time, we use a delay function in JavaScript.

There are several ways to delay the execution of a function. This can be useful for various purposes, such as creating animations, implementing debounce in search inputs, or simply delaying an action until a certain condition is met.

Using setTimeout() Method

The most common way to delay a function in JavaScript is by using the setTimeout() function. This function allows you to specify a delay (in milliseconds) after which a function will be executed.

Example: This example shows the use of the setTimeout() method to delay the function call in JavaScript. In this example, myGeeks() function will be executed after a delay of 3 seconds (3000 milliseconds).

Javascript
function myGeeks() {
    console.log("Function executed after 3 seconds");
}

setTimeout(myGeeks, 3000);

Output

Function executed after 3 seconds

Using Promises and async/await

You can also create a delay using Promises and the async/await syntax. This approach is useful when you want to use delay within an asynchronous function.

Example: In this example, the delay function returns a Promise that resolves after a specified number of milliseconds. The myGeeks uses await to pause its execution until the delay is completed.

Javascript
function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function myGeeks() {
    console.log("Waiting 2 seconds...");
    await delay(2000);
    console.log("Function executed after 2 seconds");
}

myGeeks();

Output

Waiting 2 seconds...
VM141:8 Function executed after 2 seconds

Using setInterval() for Repeated Delays

If you need to repeatedly delay a function at fixed intervals, you can use setInterval() method.

Example: In this example, mtGeeks will be executed every 1 second (1000 milliseconds).

Javascript
function myGeeks() {
    console.log("Function executed every 1 second");
}

setInterval(myGeeks, 1000);

Output

Function executed every 1 second
Function executed every 1 second
. . .

Canceling a Delay

You can cancel a delay created with setTimeout() or setInterval() using clearTimeout() and clearInterval() respectively.

Example: In this example, the execution of the function passed to setTimeout() is canceled before it has a chance to execute.

Javascript
let timeoutId = setTimeout(() => {
    console.log("This will not be executed");
}, 3000);

clearTimeout(timeoutId);

Output

Function executed every 1 second


Similar Reads

How to Delay a Function Call in JavaScript ?
Delaying a JavaScript function call involves executing a function after a certain amount of time has passed. This is commonly used in scenarios where you want to postpone the execution of a function, such as in animations, event handling, or asynchronous operations. Below are the methods to delay a JavaScript function call: Table of Content Using s
2 min read
Program to swap two integer parameters using call by value & call by address in PHP ?
Call by value: In call by value, we will send the value from the calling function to the called function. The values of the calling function arguments are directly copied to the corresponding arguments of the called function. If any modification is done on the arguments of the called function, it will not be effected on the arguments of the calling
3 min read
Call by Value Vs Call by Reference in JavaScript
Call by Value: In this method, values of actual parameters are copied to the function’s formal parameters, and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in the actual parameters of the caller. Suppose there is a variable named “a”. Now, we store a primitive value(boo
3 min read
How to execute setInterval function without delay for the first time in JavaScript ?
The setInterval() method always invokes the function after the delay for the first time using two approaches: Method 1: Calling the function once before executing setInterval: The function can simply be invoked once before using the setInterval function. This will execute the function once immediately and then the setInterval() function can be set
2 min read
How to redirect to multiple websites with a delay using JavaScript ?
We have given multiple websites and the task is to redirect to multiple websites with some delay using JavaScript. We will use setTimeout() function to delay the website. setTimeout() Function: The setTimeout() method executes a function, after waiting a specified number of milliseconds. The first parameter is a command/function to be executed and
2 min read
How to delay a loop in JavaScript using async/await with Promise ?
Given below is a JavaScript code snippet which is basically described how to delay a loop in JavaScript using async/await. In this article, we are using JavaScript either the web browser or Node.js. We all face a difficult situation in delaying a loop in JavaScript unlike C++ where we have sleep() function, but there is nothing like this in JavaScr
2 min read
How To Create a Delay Function in ReactJS ?
Delay functions in programming allow for pausing code­ execution, giving deve­lopers precise control over timing. These functions are essential for tasks such as content display, animations, synchronization, and managing asynchronous operations. In this article, we will discuss how can we create a delay function in ReactJs. Pre-requisitesIntroducti
3 min read
Underscore.js _.delay() Function
Underscore.js _.delay() function executes the mentioned function in its argument after waiting for the specified milliseconds. It is mostly used when we want to perform some task but after a certain amount of time. In this case, we can define this function, and then it will be executed after the wait milliseconds. If we pass the arguments also to t
4 min read
How to add a delay in a JavaScript loop?
JavaScript doesn't offer any wait command to add a delay to the loops but we can do so using setTimeout method. This method executes a function, after waiting a specified number of milliseconds. Below given example illustrates how to add a delay to various loops: For loop:[GFGTABS] JavaScript for (let i=0; i<10; i++) { task(i); } function task(i
3 min read
How to call a function that return another function in JavaScript ?
The task is to call a function that returns another function with the help of JavaScript is called a Currying function, a function with numerous arguments that can be converted into a series of nesting functions with the help of the currying method. Approach:Define Outer Function:Create an outer function that takes parameters and contains the logic
2 min read
three90RightbarBannerImg