pass a function as a parameter and then execute it in a jquery function

14,301

leave out the parentheses , you can then call the parameter as a function inside your "generalFunction" function.

setSomething(){
   // do other stuff  
}

generalFunction(par1, par2, par3) { 
    // do stuff...

    // you can call the argument as if it where a function ( because it is !)
    par3();
}

generalFunction(a, b, setSomething);
Share:
14,301
bobighorus
Author by

bobighorus

Updated on June 09, 2022

Comments

  • bobighorus
    bobighorus almost 2 years

    I was wondering which is the way to make this simple (and maybe stupid) thing with jQuery.

    I have a function like this:

    function setSomething() { 
        make some stuff; 
    }
    

    and then another function like this:

    generalFunction(par1, par2, par3) { 
        do other stuff; 
        execute function called in par3;    
    }
    

    Well, if I write something like this it doesn't work:

    c=setSomething(); 
    generalFunction(a, b, c);
    

    So what's the way to call a function as a parameter of another function and then execute it inside?

    I hope I was clear enough.

    Any help will be appreciated.

    Thank you in advance for your attention.