Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript: Can someone explain how these parameters are correct?

I’m getting into Javascript functions with parameters taking functions as parameters. I understand passing parameters, but I’ve seen the following example, or examples like it:

// Callback Function Example
function greet(name, myFunction) {
    console.log('Hello world');

    // callback function
    // executed only after the greet() is executed
    myFunction(name);
}

// callback function
function sayName(name) {
    console.log('Hello' + ' ' + name);
}

// calling the function after 2 seconds
setTimeout(greet, 2000, 'John', sayName);

It would seem to me you would call it:

setTimeout(greet('John', sayName), 2000);

But I’m pretty sure that is not right.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can someone explain the reasoning of the parameter passing in that?

Thanks,

Russ

>Solution :

Please check the below link to have more deeper understanding of SetTimeout

setTimeout takes multiple parameters,

setTimeout(functionRef, delay, ...arguments )

functionRef : Function to be executed

delay: in milliseconds

arguments: can be any number of parameters which will passed to the function ref

https://developer.mozilla.org/en-US/docs/Web/API/setTimeout

Example:

  setTimeout((name,age,city)=>{alert(name+' '+age+' '+ city)},2000,"ABC","1","Dubai")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading