How can I pass arguments to the callback function in setTimeout

0 votes
With the help of a proper code explanation, can you tell me How I can pass arguments to the callback function in setTimeout()?
Jan 10 in Java-Script by Ashutosh
• 17,360 points
51 views

1 answer to this question.

0 votes

You can pass arguments to the callback function in setTimeout() by using anonymous functions or arrow functions.

1. Using an anonymous function (or arrow function):

You can pass arguments by wrapping your callback in an anonymous function or an arrow function, which then calls the actual callback with the desired arguments.

Example:

function greet(name) {

  console.log('Hello, ' + name);

}

// Using setTimeout with an anonymous function

setTimeout(function() {

  greet('Alice');

}, 1000);

// Or using an arrow function

setTimeout(() => greet('Bob'), 2000);

2. Using bind() method:

You can also use the bind() method to pre-assign arguments to your callback function before passing it to setTimeout().

Example:

function greet(name) {

  console.log('Hello, ' + name);

}

setTimeout(greet.bind(null, 'Charlie'), 3000);

answered Feb 7 by Navya

Related Questions In Java-Script

0 votes
1 answer

How can I update NodeJS and NPM to the next versions?

Hello @kartik, First check your NPM version npm -v 1).Update ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,840 points
1,197 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,840 points
3,430 views
0 votes
1 answer

How can I pass PHP objects to javascript?

Hello @kartik, You can convert the PHP object ...READ MORE

answered Jul 7, 2020 in Java-Script by Niroj
• 82,840 points
5,899 views
0 votes
1 answer

How do I copy to the clipboard in JavaScript?

Hello @kartik, To copy HTML , you can ...READ MORE

answered Aug 28, 2020 in Java-Script by Niroj
• 82,840 points
951 views
0 votes
1 answer

How can I iterate through keys and values in JavaScript?

In JavaScript , you can iterate through ...READ MORE

answered Feb 7 in Java-Script by Navya
59 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How can I use jQuery to select all elements with multiple CSS classes?

You can use the following syntax to ...READ MORE

answered Dec 17, 2024 in Java-Script by Navya
65 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP