Underscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects.
The _.after() function is an inbuilt function in Underscore.js library of JavaScript which is used to create a wrapper of function which doesn’t do anything in the beginning but from the stated count it starts calling the stated function. Moreover, it is useful in a grouping of asynchronous responses, as you need to be sure if all the async calls have ended or not before preceding.
Syntax:
_.after(count, function)
Parameters: It accepts two parameters which are specified below:
- count: It is the number of counts after which the stated function will be called.
- function: It is the function stated.
Return values: This method returns the count of the function called.
Example 1:
<!DOCTYPE html>
<html>
<head>
<script src=
</script>
</head>
<body>
<script>
show = () => {
console.log( "GeeksforGeeks" )
}
console.log(`Show function will run for 5 times:`)
func = _.after(3, show);
func();
func();
func();
func();
func();
func();
func();
</script>
</body>
</html>
|
Output:
Show function will run for 5 times:
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
Example 2:
<!DOCTYPE html>
<html>
<head>
<script src=
</script>
</head>
<body>
<button id= "button" >button</button>
<script>
show = () => {
console.log( "GeeksforGeeks" )
}
func = _.after(3, show);
let button = document.querySelector( "#button" );
let Run = () => {
console.log(`Show function runs for 8 times:`)
for (let i = 0; i < 10; i++) {
func();
}
}
button.addEventListener( "click" , Run)
</script>
</body>
</html>
|
Output:
button
Show function runs for 7 times:
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
Here, you need to click on the button in order to see the output.
Reference: https://underscorejs.org/#after
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!
Last Updated :
25 Nov, 2021
Like Article
Save Article