Syntax, Parameter and Return Value
Syntax:
_.delay(func, wait, [...args])
Parameters:
-
func (Function): The function to delay.
-
wait (number): The number of milliseconds to delay invocation.
- [...args]: The arguments to invoke the function with.
Return Value:
(number) - Returns the timer ID which can be used to cancel the delay.
Examples
Basic Delayed Function Execution:
JavaScript
var _ = require('lodash');
function greet(name) {
console.log('Hello, ' + name);
}
_.delay(greet, 1000, 'John');

You can also try this code with Online Javascript Compiler
Run Code
Output after 1 second:
'Hello, John'
Demonstrates delaying a greeting function by 1 second.
Delaying a Task with Multiple Arguments:
JavaScript
function logEvent(eventType, message) {
console.log(eventType + ': ' + message);
}
_.delay(logEvent, 2000, 'UserLogin', 'User logged in successfully');

You can also try this code with Online Javascript Compiler
Run Code
Output after 2 seconds:
'UserLogin: User logged in successfully'
Shows delaying a function with multiple arguments.
Using Delay for Throttling Behavior:
JavaScript
var saveData = function(data) {
console.log('Data saved:', data);
};
document.getElementById('saveButton').addEventListener('click', () => {
_.delay(saveData, 500, 'User data');
});

You can also try this code with Online Javascript Compiler
Run Code
// Delays the saveData function by 500ms on button click.
An example of using _.delay() to throttle a button click handler.
Scheduling Multiple Delayed Calls:
JavaScript
function reminder(message) {
console.log(message);
}
_.delay(reminder, 1000, 'Reminder 1');
_.delay(reminder, 3000, 'Reminder 2');

You can also try this code with Online Javascript Compiler
Run Code
Output after 1 second:
'Reminder 1'
Output after 3 seconds:
'Reminder 2'
Demonstrates scheduling multiple functions with different delays.
Frequently Asked Questions
How does _.delay() differ from the native setTimeout?
_.delay() offers a syntax that is more aligned with functional programming and is more readable, especially when dealing with multiple arguments for the delayed function.
Can the delay be cancelled once set with _.delay()?
Yes, the delay can be cancelled using clearTimeout, as _.delay() returns a timer ID similar to setTimeout.
Is _.delay() appropriate for time-sensitive applications?
While useful for basic timing needs, _.delay(), like setTimeout, is not guaranteed to execute precisely at the specified time due to JavaScript's single-threaded nature and event loop behavior.
Conclusion
Lodash's _.delay() method is a convenient and readable way to handle delayed function execution, offering enhanced syntax and functionality over the native setTimeout. It's especially useful for scheduling tasks, throttling event handlers, or simply delaying function calls.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.