11

I am not so strong in javascript.

I have a common function that I call from many parts of my code passing them some parameters.

Can somebody help me on

  • how to define a new parameter for this function that should be a callback with no parameters passed from the caller (like many jquery plugins do)
  • how to handle the callback call inside the function

Giving advice regarding the solution, if there's a better one, etc.

thanks a lot!

1 Answer 1

20

It is actually quite simple.

function callback() {
    alert("I am in the callback!");
}

function work(func) {
    alert("I am calling the callback!");
    func(); 
}

work(callback);
Sign up to request clarification or add additional context in comments.

2 Comments

Does it work even if the work and the callback are defined in different js files?
@Lorenzo - As long as both JS files have been loaded by the run-time it will work. Don't take my word for it though, try it out.

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.