Open In App

Underscore.js _.delay() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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 this function (which is optional to pass) then these arguments will act as the argument of the function passed to the _.delay() function.

Syntax:

_.delay(function, wait, *arguments);

Parameters:

  • function: The function to be executed.
  • wait: The time after which the function needs to be executed ( in milliseconds)
  • *arguments: The argument for the function passed to the _.delay() function (optional)

Return value:

It returns the values of the function passed, being executed after waiting a millisecond.

Passing the function directly to the _.delay() function: 

The _.delay() function takes the wait parameter which here is 1000ms, then waits for 1000ms, and then executes the function passed which here is console.log() and prints the string passed to it, i.e., coding is fun. So, after 1000ms the string “coding is fun” will be displayed.

Example: The below code example implements the _.delay() function of underscore.js.

html




<!DOCTYPE html>
<html>
 
<head>
    <!--
        These lines are for Mozilla Firefox
        developer edition to stop the web packs
    -->
    <meta http-equiv="Content-Type" content=
"text/html;charset=utf-8">
    <meta content="utf-8" http-equiv="encoding">
     
    <!-- You may ignore these when using in another browser -->
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        _.delay(console.log, 1000, 'coding is fun!');
    </script>
</body>
 
</html>


Output:

coding is fun!

Using _.bind() Function with the _.delay() function:

The _.bind() function is used to pass the object to the function. Like here the console.log() function has an object console. This ‘func()’ means that whatever will be passed to this function will be displayed on the console. No waiting time is mentioned in the _.bind() function. Then in the _.delay() function we need to wait 2000 msec and after that the string “hello” will be displayed onto the console.

Example: The below code example uses the _.delay() method with the _.bind() method.

html




<!DOCTYPE html>
<html>
 
<head>
    <!--
        These lines are for Mozilla Firefox
        developer edition to stop the web packs
    -->
    <meta http-equiv="Content-Type" content=
"text/html;charset=utf-8">
    <meta content="utf-8" http-equiv="encoding">
    <!-- You may ignore these when using in another browser -->
    <script src=
    </script>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        const func = _.bind(console.log, console);
        _.delay(func, 2000, 'hello');
    </script>
</body>
 
</html>


Output:

hello

Passing more than just one argument to the function passed to _.delay() function:

The _.delay() function has a ‘func()’ passed to it which contains the same _.bind() function as in the earlier example. Then a waiting time of 3000 msec is passed which means the output will be displayed after 3000 msec. There are 3 more parameters passed which will be considered as the parameters of the ‘func()’ function passed. Therefore, the final output will be displayed after 3000 msec and will be a combination of all the 3 strings, i.e., “hello! how are you?”.

Example: The below code will implement the _.delay() function with more than one parameter.

html




<!DOCTYPE html>
<html>
 
<head>
    <!--
        These lines are for Mozilla Firefox
        developer edition to stop the web packs
    -->
    <meta http-equiv="Content-Type" content=
"text/html;charset=utf-8">
    <meta content="utf-8" http-equiv="encoding">
 
    <!-- You may ignore these when using in another browser -->
    <script src=
    </script>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        const func = _.bind(console.log, console);
        _.delay(func, 3000, 'hello!', 'how are', 'you?');
    </script>
</body>
 
</html>


Output:

hello! how are you?

Passing a number as the parameter to the passed function to the _.delay() function:

We can even pass numbers as argument tot he passed function. Here, we are passing ‘12345’ as the argument to the ‘func()’ function. The ‘func()’ function is declared as it is in earlier examples. The output of this function will be “12345” which will be displayed after 5000msec.

Example: The below code example implements the _.delay() function in the specified scenario.

html




<!DOCTYPE html>
<html>
 
<head>
    <!--
        These lines are for Mozilla Firefox
        developer edition to stop the web packs
    -->
    <meta http-equiv="Content-Type" content=
"text/html;charset=utf-8">
    <meta content="utf-8" http-equiv="encoding">
 
    <!-- You may ignore these when using in another browser -->
    <script src=
    </script>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        const func = _.bind(console.log, console);
        _.delay(func, 5000, '12345');
    </script>
</body>
 
</html>


Output:

12345


Last Updated : 16 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads