How to Delay a JavaScript Function Call using JavaScript ?
Last Updated :
14 Jun, 2025
Delaying the execution of a JavaScript function is a powerful technique often achieved using methods like setTimeout()
. This approach is commonly used in over 60% of modern web applications to manage animations, control user interactions, or handle asynchronous behavior smoothly. Whether it's for debouncing a search input, adding delay to UI transitions, or executing a function after a condition is met, delaying function calls ensures better performance and an enhanced user experience.
Using setTimeout() Method
The setTimeout() method delays a function's execution by a specified time in milliseconds. It takes a callback function and a delay value, running the function once after the delay elapses.
Example 1: 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).
script.js
function myGeeks() {
console.log("Geeks for Geeks statement will be executed in 3 seconds");
}
setTimeout(myGeeks, 3000);
Output
Function executed after 3 seconds
Using Promises and async/await
Using Promises and async/await, you can delay a function by creating a Promise that resolves after a set time with setTimeout(). Use await to pause execution until the delay completes.
Example 2: 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.
script.js
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
The setInterval() method repeatedly executes a function at specified intervals in milliseconds until cleared. Unlike setTimeout(), it runs continuously at set intervals, ideal for tasks requiring consistent updates, like animations or periodic data fetching.
Example 3: In this example, mtGeeks will be executed every 1 second (1000 milliseconds).
script.js
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
To cancel a delayed function call set by setTimeout(), use the clearTimeout() method. Pass the identifier returned by setTimeout() to stop the scheduled function from executing, effectively canceling the delay.
Example 4: In this example, the execution of the function passed to setTimeout() is canceled before it has a chance to execute.
script.js
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
2 min read
How to measure time taken by a function to execute using JavaScript ? This article will show how to measure the time taken by a function to execute using Javascript. To measure the time taken by a function to execute we have three methods: Table of Content Using the Using Date ObjectUsing the performance.now() methodUsing the console.time() methodMethod 1: Using the U
3 min read
How to call a function repeatedly every 5 seconds in JavaScript ? In JavaScript, the setInterval() method allows you to repeatedly execute a function or evaluate an expression at specified intervals. This method is particularly useful for performing periodic tasks like updating a user interface or making repeated API calls.Syntax:Â setInterval(function, millisecond
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 d
3 min read
How to create an Asynchronous function in Javascript? JavaScript is a single-threaded and synchronous language. The code is executed in order one at a time. But Javascript may appear to be asynchronous in some situations. Example: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title
6 min read