How do I write a jquery function that accepts a callback as a parameter

后端 未结 4 1469
礼貌的吻别
礼貌的吻别 2021-01-30 03:08

I Have the following function.

function ChangeDasPanel(controllerPath, postParams) {

    $.post(controllerPath, postParams, function(returnValue) {

        $(         


        
4条回答
  •  半阙折子戏
    2021-01-30 03:54

    You know that global variables and functions are evil, so why not put your's into the jQuery namespace:

    $.extend({
      myFunc : function(someArg, callbackFnk){
        var url = "http://example.com?q=" + someArg;
        $.getJSON(url, function(data){
    
          // now we are calling our own callback function
          if(typeof callbackFnk == 'function'){
            callbackFnk.call(this, data);
          }
    
        });
      }
    });
    
    $.myFunc(args, function(){
      // now my function is not hardcoded
      // in the plugin itself
    });
    

    Read this post to get a better understanding: Creating callback functions in jQuery

提交回复
热议问题