javascript 在javascript中调用回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3841454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:26:13  来源:igfitidea点击:

Calling a callback in javascript

javascriptcallback

提问by Lorenzo

I am not so strong in javascript.

我在 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
  • 如何为这个函数定义一个新参数,它应该是一个没有从调用者传递参数的回调(就像许多 jquery 插件所做的那样)
  • 如何处理函数内部的回调调用

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

提供有关解决方案的建议,如果有更好的解决方案等。

thanks a lot!

多谢!

回答by ChaosPandion

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);