Underscore.js | _.delay() with Examples

_.delay() function:

  • It 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: It takes three arguments:



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

Return value: It returns the values of the function passed, being executed after wait millisecond.

Examples:

  1. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
       
    <head>
        <!-- These lines are for Mozilla Firefox 
              developer edition to stop the web packs-->
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <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>

    chevron_right

    
    

    Output:

  2. Using _.bind() Function with the _.delay() function:
    _.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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
       
    <head>
        <!-- These lines are for Mozilla Firefox 
           developer edition to stop the web packs-->
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <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">
            var func = _.bind(console.log, console);
            _.delay(func, 2000, 'hello');
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  3. 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 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 string, i.e., “hello! how are you?”.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
       
    <head>
        <!-- These lines are for Mozilla Firefox 
           developer edition to stop the web packs-->
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <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">
            var func = _.bind(console.log, console);
            _.delay(func, 3000, 'hello!', 'how are', 'you?');
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  4. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
       
    <head>
        <!-- These lines are for Mozilla Firefox 
             developer edition to stop the web packs-->
        <meta content="text/html;charset=utf-8" 
         http-equiv="Content-Type">
        <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">
            var func = _.bind(console.log, console);
            _.delay(func, 5000, '12345');
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

NOTE: These commands will not work in Google console or in firefox as for these additional files need to be added which they didn’t have added.
So, add the given links to your HTML file and then run them.

The links are as follows:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!-- Write HTML code here -->
<script type="text/javascript" src =
</script>

chevron_right




My Personal Notes arrow_drop_up

I like to do coding in C++C and java programming languages HTML and CSS always intersts me Sharing knowleged is the best way according to me to increase ones knwoledge

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.