Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a callback as a parameter into another function

I'm new to ajax and callback functions, please forgive me if i get the concepts all wrong.

Problem: Could i send a callbackfunction as a parameter to another function that will execute the callback?

function firstFunction(){     //some code      //a callback function is written for $.post() to execute     secondFunction("var1","var2",callbackfunction); }  function secondFunction(var1, var2, callbackfunction) {     params={}     if (event != null) params = event + '&' + $(form).serialize();      // $.post() will execute the callback function     $.post(form.action,params, callbackfunction); } 
like image 520
Wee Kiat Avatar asked Jun 24 '11 09:06

Wee Kiat


People also ask

How do you pass a callback function as a parameter?

Passing a function to another function or passing a function inside another function is known as a Callback Function. Syntax: function geekOne(z) { alert(z); } function geekTwo(a, callback) { callback(a); } prevfn(2, newfn); Above is an example of a callback variable in JavaScript function.

Can a callback function call another function?

Yes. The print( ) function takes another function as a parameter and calls it inside. This is valid in JavaScript and we call it a “callback”. So a function that is passed to another function as a parameter is a callback function.

Can a callback function take parameters?

Using the Array. forEach() method as an example, the callback function will always receive the current item, its index, and the array itself as arguments, in that order. You can name the parameters for them anything you want, or even omit them entirely, but the method will always pass them in as arguments.

What is callback function parameter?

In JavaScript, when we pass a function to another function as a parameter, it is called a callback function. The function takes another function as a parameter and calls it inside. A callback function makes sure that a function will not run until the task completes.


2 Answers

Yup. Function references are just like any other object reference, you can pass them around to your heart's content.

Here's a more concrete example:

function foo() {     console.log("Hello from foo!"); }  function caller(f) {     // Call the given function     f(); }  function indirectCaller(f) {     // Call `caller`, who will in turn call `f`     caller(f); }  // Do it indirectCaller(foo); // logs "Hello from foo!"

You can also pass in arguments for foo:

function foo(a, b) {     console.log(a + " + " + b + " = " + (a + b)); }  function caller(f, v1, v2) {     // Call the given function     f(v1, v2); }  function indirectCaller(f, v1, v2) {     // Call `caller`, who will in turn call `f`     caller(f, v1, v2); }  // Do it indirectCaller(foo, 1, 2); // logs "1 + 2 = 3"
like image 65
T.J. Crowder Avatar answered Sep 18 '22 15:09

T.J. Crowder


Also, could be simple as:

if( typeof foo == "function" )     foo(); 
like image 28
Brad Avatar answered Sep 18 '22 15:09

Brad