How to pass a callback as a parameter into another function

后端 未结 6 1721
生来不讨喜
生来不讨喜 2020-11-28 03:32

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

Problem: Could i send a callbackfunction

6条回答
  •  盖世英雄少女心
    2020-11-28 04:13

    Yes of course, function are objects and can be passed, but of course you must declare it:

    function firstFunction(){
        //some code
        var callbackfunction = function(data){
           //do something with the data returned from the ajax request
         }
        //a callback function is written for $.post() to execute
        secondFunction("var1","var2",callbackfunction);
    }
    

    an interesting thing is that your callback function has also access to every variable you might have declared inside firstFunction() (variables in javascript have local scope).

提交回复
热议问题