0

Please refer to my below code:

//Principal Function
function greet3(name, sayName) {
  document.getElementById("jac3").innerHTML = "Hello " + name;
  sayName(name);
};
//Callback Function

function sayName(name) {
  document.getElementById("jac3").innerHTML += ". How are you? " + name;
}
//Calling the function after 10 seconds

greet3("Neha", setTimeout(sayName,3000));
//greet3("Neha", sayName);
<p id="jac3"></p>

When I use setTimeout function,

greet3("Neha", setTimeout(sayName,3000));

the output of name gets undefined.

Please help me with correct syntax.

1 Answer 1

3

When you write setTimeout(param1, param2) you are executing the function due to the parenthesis.

//Principal Function
function greet3(name, callback, timeout) {
  document.getElementById("jac3").innerHTML = "Hello " + name;
  setTimeout(() => callback(name), timeout);
};

//Callback Function
function sayName(name) {
  document.getElementById("jac3").innerHTML += ". How are you? " + name;
}

//Calling the function after 3 seconds
greet3("Neha", sayName, 3000);
<p id="jac3"></p>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.