Underscore.js _.delay() Function
The _.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.
_.delay(function, wait, *arguments)
Parameters: It takes three arguments:
- function: The function to be executed.
- wait: The time after which the function needs to be executed ( in millisecond)
- *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 wait 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.
- 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.
- 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?”.
- 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.
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:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!