jQuery Plugin: Adding Callback functionality

后端 未结 6 1787
孤街浪徒
孤街浪徒 2020-11-28 00:40

I\'m trying to give my plugin callback functionality, and I\'d like for it to operate in a somewhat traditional way:

myPlugin({options}, function() {
    /*          


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 01:18

    Change your plugin function to take a second parameter. Assuming that the user passes a function, that parameter can be treated as a regular function.
    Note that you can also make the callback a property of the options parameter.

    For example:

    $.fn.myPlugin = function(options, callback) {
        ...
    
        if(callback)        //If the caller supplied a callback
            callback(someParam);
    
        ...
    });
    

提交回复
热议问题