在javascript中调用回调 [英] Calling a callback in javascript

查看:109
本文介绍了在javascript中调用回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中没有那么强。



我有一个通用函数,我从代码的很多部分传递他们一些参数。



有人可以帮助我




  • 如何为此定义新参数

  • 如何处理函数内部的回调调用

  • ul>

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




    解决方案

    其实很简单。

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

    function work(func){
    alert(我正在调用回调!
    func();
    }

    work(callback);


    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!

    解决方案

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

    这篇关于在javascript中调用回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆