Explain what additional parameters are passed to the setTimeout function, in addition to the time and function that will be executed? Preferably with a little example to understand.

    1 answer 1

    Additional parameters are arguments for the function.

    setTimeout(f, 1000, 10, 'string', false); function f(integer, string, bool) { console.log(integer, string, bool); } 

    log


    Not only values ​​can be in the argument

     setTimeout(f, 1000, 'курочка', function(doing) { console.log(doing); }); function f(chicken, todo) { todo(chicken + ' снесла яйко'); } 

    chicken

    • that is, in the second example, the parameter itself is passed to the function itself, which will be executed in timeout, and another function is passed in turn, and then we can use it in the body of the timeout function, I understood correctly? - Artem Palamarchuk
    • @ArtemPalamarchuk, that's it. This is called a callback - Mr. Black
    • function that is passed as an argument to another function? I'm just trying to understand everything very well, so I ask - Artem Palamarchuk
    • @ArtemPalamarchuk is a callback function. The executable code is passed as an argument. Tuts . The theoretically transmitted function works in the body of f , and not setTimeout - Mr. Black